我有以下内容:
main.php:
spl_autoload_register(function($className) {
require_once $className . '.php';
});
use Example\RandomClass;
include 'anotherFile.php';
anotherFile.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
。这是预期的行为吗?我应该如何做到这一点,以便我只需要一次课程?
答案 0 :(得分:0)
我相信您的自动加载是错误的:
require_once $className . '.php';
应该是
require_once 'Example\' . $className . '.php';
并检查 this answer