【发布时间】:2015-03-29 03:48:53
【问题描述】:
我有类似以下的内容:
main.php:
spl_autoload_register(function($className) {
require_once $className . '.php';
});
use Example\RandomClass;
include 'anotherFile.php';
另一个文件.php:
RandomClass::example();
示例/RandomClass.php:
namespace Example;
class RandomClass {
public static function example() {
echo 'example';
}
}
但是我收到以下警告:
Warning: require_once(RandomClass.php): failed to open stream: No such file or directory in main.php on line 5 除非我在 anotherFile.php 中也有 use Example\RandomClass.php。这是预期的行为吗?我应该如何做到这一点,以便我只需要上课一次?
【问题讨论】:
-
为什么不使用命名空间感知自动加载器 - php-fig.org/psr/psr-4
标签: php namespaces include spl-autoload-register