[英]PHP classes with a namespace cannot be loaded via spl_autoload_register?
[英]spl_autoload_reqister classes not getting loaded
我有一個看起來像的文件夾結構
base_dir-
Includes.php
Libs-
Database.php
Log.php
Cofing.php
Models-
someClass.php
Scheduled-
test.php
我的Includes.php
有
spl_autoload_register(NULL, FALSE);
spl_autoload_extensions('.php, .class.php, lib.php');
function libLoader($name) {
$file = 'Libs/' . $name . '.php';
if (!file_exists($file)) {
// throw new Exception("Error Loading Library: $file does not exists!", 1);
return FALSE;
}
require_once $file;
}
function modelLoader($name) {
$file = 'Models/' . $name . '.php';
if (!file_exists($file)) {
// throw new Exception("Error Loading Library: $file does not exists!", 1);
return FALSE;
}
require_once $file;
}
spl_autoload_register('libLoader');
spl_autoload_register('modelLoader');
我的someClass.php
有
require_once '../Includes.php';
class someClass extends Database
{
public function __construct() { return 'hello world'; }
}
和test.php
有
require_once '../Includes.php';
try {
$loads = new someClass();
} catch (Exception $e) {
echo "Exception: " . $e->getMessage();
}
當我運行test.php
在... / Scheduled / test.php上找不到someClass
spl是否可與someClass.php之類的擴展類一起使用,還是我需要包括要擴展的類?
為什么不找到someClass.php
?
謝謝
更改
$file = 'Models/' . $name . '.php';
至
$file = __DIR__ . '/Models/' . $name . '.php';
在模型自動加載器(以及libLoader中的等效文件)中,以確保它是從正確的目錄而不是從test.php文件所在的目錄中搜索
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.