【发布时间】:2012-12-02 11:43:18
【问题描述】:
我有一个 PHP 项目,结构如下:
--root
|--dr1
| |---dr2
| |--testclass.php
|--start.php
|--bootstrap.php
testclass.php 包含:
namespace dr1\dr2;
class testclass {
...
}
bootstrap.php 包含:
define('DIR_SEP', DIRECTORY_SEPARATOR);
define('ROOT', dirname(__FILE__) . DIR_SEP);
function __autoload($class)
{
$path = ROOT . str_replace('\\', DIR_SEP, $class);
$file = $path . '.php';
if( is_file($file) ) require_once($file);
}
spl_autoload_extensions('.php');
spl_autoload_register('__autoload');
并且 start.php 包含:
$class = 'dr1\dr2\testclass.php';
$obj = new $class();
当我运行 start.php 时,我收到消息 dr1\dr2\testclass.php is not found on start.php on line 5。我不知道为什么。有人会帮忙吗?非常感谢。
【问题讨论】:
-
该类名为
testclass而不是testclass.php。所以我猜应该是:$class = 'dr1\dr2\testclass'. -
您的答案有效,发布它,我会接受。 :) 谢谢。
-
哇,我见过第一个知道答案但没有尝试在答案中回答的人。
标签: php namespaces autoload