我想自動載入類,但spl_autoload寄存器沒有找到該文件,我收到文件未找到錯誤。Spl_autoload_register文件未找到
我的文件結構:
index.php
system/
configs/
config.php
messages/
message.php
user/
user.php
因此,這裏是我在做什麼。
的config.php
我從另一個答案了以下功能
spl_autoload_register(function ($className) {
# Usually I would just concatenate directly to $file variable below
# this is just for easy viewing on Stack Overflow)
$ds = DIRECTORY_SEPARATOR;
$dir = __DIR__;
// replace namespace separator with directory separator (prolly not required)
$className = str_replace('\\', $ds, $className);
// get full name of file containing the required class
$file = "{$dir}{$ds}{$className}.php";
require_once $file;
});
use system\user\user;
$obj = new User();
user.php的
namespace system\user;
class user{
...
}
我收到以下錯誤:
Fatal error: require_once(): Failed opening required 'C:\xampp\htdocs\System\Configs\System\User\User.php' (include_path='.;C:\xampp\php\PEAR') in
任何幫助?
不起作用。 '警告:require_once(C:\ xampp \ htdocs \ System \ Configs /../../ \ System \ User \ Exception.php):未能打開流:沒有這樣的文件或目錄' – Magna
'System \ User \ Exception .php'存在嗎? – Federkun
我沒有那個文件 – Magna