我目前正在构建一个需要几个不同名称空间的应用程序,但是我希望将大部分应用程序放在app命名空间下。
在我的index.php文件中,我声明要使用app命名空间,但是PHP仍然坚持我的类不能像这样找到......
<?php
define('PATH',strtok ( $_SERVER['REQUEST_URI'] , '?' ));
define("ROOT",$_SERVER['DOCUMENT_ROOT'].'/',true);
require 'vendor/autoload.php';
require 'app/class_loader.php';
use Carbon\Carbon;
use app\routing;
$routing = new routing;
$routing->router();
在我的路由类中,我将它声明为应用程序命名空间的一部分,如此
<?php
namespace app;
class routing
{
但是我一直得到Fatal error: Class 'app\routing' not found in /var/www/my_app/index.php on line 15
这是我的自动加载文件
<?php
function autoload_class_multiple_directory($class_name)
{
# List all the class directories in the array.
$array_paths = array(
'app/controllers',
'app/models',
'app/services',
);
foreach($array_paths as $path)
{
$file = $path.'/'.$class_name.'.php';
if(is_file($file))
{
include $file;
}
}
}
spl_autoload_register('autoload_class_multiple_directory');
我认为命名空间的工作原理是什么?我想确保我的应用程序易于理解并从一开始就编写代码,请有人帮我指点正确的方向吗?
喝彩!