Классы spl_autoload_reqister не загружаются
У меня есть структура папок, которая выглядит как
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
Работает ли spl с расширенными классами, такими как someClass.php, или мне нужно включить класс для расширения?
И почему бы его не найти someClass.php
?
Спасибо
1 ответ
Решение
+ Изменить
$file = 'Models/' . $name . '.php';
в
$file = __DIR__ . '/Models/' . $name . '.php';
в вашем автозагрузчике моделей (и аналогичном в вашем libLoader), чтобы убедиться, что он ищет в правильном каталоге, а не в каталоге, где находится файл test.php