【发布时间】:2015-10-07 09:52:22
【问题描述】:
我无法让自动加载与命名空间一起使用。这是我为此创建的目录结构:
index.php
app/
utils/
sys/
DirReader.php
helpers/
DB.php
index.php 包含自动加载器,其中包括文件 DirReader.php 和 DB.php。
index.php 是这样的:
<?php
function __autoload($ns_str) //ns_str = namespace string
{
$path = str_replace('\\', DIRECTORY_SEPARATOR, $ns_str);
//echo "**$path**\n";
require_once "$path.php";
}
use \app\utils\sys as sys;
use \app\utils\helpers as helpers;
$dir = new sys\DirReader();
$db = new helpers\DB();
这里是DirReader.php:
<?php
namespace app\utils\sys;
class DirReader
{
public function __construct()
{
echo "DirReader object created!\n";
}
}
这里是DB.php:
<?php
namespace app\utils\helpers;
class DB
{
public function __construct()
{
echo "DB object created!\n";
}
}
该示例运行良好,但是当我向index.php 添加命名空间声明时,它失败了:
<?php
namespace myns;
function __autoload($ns_str) //ns_str = namespace string
{ /*. . .*/
PHP 致命错误:在中找不到类 'app\utils\sys\DirReader' /var/www/html/php_learn/autoloading_1/index.php 第 15 行 PHP 堆栈 跟踪:PHP 1. {main}() /var/www/html/php_learn/autoloading_1/index.php:0
在我看来,这个错误不应该出现,因为我在 index.php 中使用命名空间时使用了绝对名称。我知道说像use app\utils\sys as sys; 这样的话会失败,因为这样命名空间将相对于myns 进行搜索,其中不存在任何内容。但我不知道为什么我的代码不起作用。 (我也尝试将index.php 中的命名空间名称更改为autoloading_1,即包含目录的名称,但没有帮助。
【问题讨论】:
标签: php