I have one giant project where all classes are autoloaded with spl_autoload_register
spl_autoload_register(function ($className) {
try {
require_once __DIR__ . DIRECTORY_SEPARATOR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . ".php";
} catch (Exception $exception) {
}
});
and all works just fine. I have some "modules" in project (each module in own folder) of which some have Administrative functions and some of them have menus to be displayed in admin panel (located in "manage" submenu of folder and class is always Admin.php).
foreach (glob(_modules_Path . "*") as $module) {
if (is_dir($module)) {
$classname = "vojjin\\modules\\" . basename($module) . "\\manage\\Admin";
try {
if (class_exists($classname)) {
if (method_exists($classname, "getAdminMenu")) {
$out .= (new $classname())->getAdminMenu();
}
}
} catch (\Exception $e) {}
}
}
When I check if that module has appropriate class, I receive an error pointed to spl_autoload require_once row fired from calling class_exists function:
Fatal error: require_once(): Failed opening required '.......\vojjin\modules\articlelist\manage\Admin.php' (include_path='.;C:\php\pear') in ...
I have tried to add a folder "manage" in that particular module (articlelist) but nothing happens. As you can see there are try/catch statements, but PHP is just not listening to me and keep throwing errors. Any ideas?
CodePudding user response:
I think replacing the try catch with an if file_exists should work
spl_autoload_register(function ($className) {
if(file_exists(__DIR__ . DIRECTORY_SEPARATOR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . ".php"){
require_once __DIR__ . DIRECTORY_SEPARATOR . str_replace("\\", DIRECTORY_SEPARATOR, $className) . ".php";
}
});