php - Throwing Exceptions in an SPL autoloader? -
Is there a way to throw an exception in PHP with a SPL Autoloader that fails it? It does not work under PHP 5.2.11.
class SPLAutoLoader {public static function autoloadDomain ($ className) {if (file_exists ('test /' class classname '.php')) {require_once ('test /'. Class classname 'Class.php' '); Return true; } New exception throw ('file not found'); }} // and class // start spl_autoload_register (array ('spile ooLoader', 'autoloidman')); Try {$ Domain = new foobarDomain (); } Hold (exception $ c) {echo 'file not found'; }
When the above code is called, there is no indication of an exception, instead I get a standard "fatal error: Class 'faubarddomain' not in the block met". And the execution of the script ends.
This is not a bug, it is:
Note : Exceptions throwing in the
__autoload
function can not be caught in thehold
block and as a result of a serious error.
The reason for this is that there can be more than one autoload handler, in this case, you do not want to bypass the handler first and bypass the second handler. You want your other handler to have an opportunity to automatically load their classes. If you use a library that uses the automatic loading feature, then you do not want to bypass your auto-load handler because they throw exceptions inside your auto loader.
If you want to see that you have a class, then another argument (or leave it, true
is the default) as true
Use and pass:
if (class_exists ('phobardman', $ autoload = true)) {$ domain = new foobarDomain (); } Else {echo 'class not found'; }
Comments
Post a Comment