|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-05-19 16:16 UTC] himself at alexanderjank dot de
Description:
------------
Today I want to request the feature of having a function callback (callable) in case all autoloaders on the SPL-Autoloader-Stack failed.
Personally, I'll find it useful, to trow an Exception when all the autoloaders weren't able to load the class. For me a workaround is, the register a last SPL-Autoload function, in which I throw the Exception (the autoloaders before my last wren't successful, if my function runs). But I don't thing, that this is a semantically correct way to do this. In my opinion, something like a error callback should exist.
Greetings from Germany,
Alexander Jank
Test script:
---------------
<?php
class ClassNotFoundException extends \RuntimeException{
public function __construct($className) {
parent::__construct(sprintf(
'Class "%s" does not exist',
$className
));
}
}
//Workaround
function MainAutoloader($class) {
//look for the class and include it (if file exists)
if(class_exists($class)) {
return true;
} else {
return false; /* next autoloader will be called */
}
}
function MainAutoloadErrorHandler($class) {
throw new ClassNotFoundException($class);
}
spl_autoload_register('MainAutoloader');
spl_autoload_register('MainAutoloadErrorHandler');
?>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 23:00:02 2025 UTC |
In PHP 7, trying to instantiate a class that is not present will throw an exception: try { $foo = new Bar(); } catch(\EngineException $e) { echo "Missing class ".$e->getMessage(); } (the actual name of the exception name may change). Presumably your workaround will tide you over until that's released?