note 73561 deleted from language.oop5.autoload by cmb
| From: | cmb@php.net | Date: | Sun, 26 May 2019 09:11:03 +0000 |
| Subject: | note 73561 deleted from language.oop5.autoload by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211131@lists.php.net to get a copy of this message | ||
Note Submitter: Klaus Schneider
----
I just stumbled over one quite nice solution to the __autoload-exception problem. It allows for any
kind of exception to be thrown inside __autoload().
It appears one has to define the requested class (using "eval", which is not nice but
inevitable here) and after that can simply throw an exception (and catch it if so desired):
<?php
function __autoload($className)
{
// Do your stuff to load a class here, set $ok if everything went fine.
if (! $ok) {
eval("class $className{};");
throw new Exception('My message');
} // if
}
try {
UndefinedClass::undefinedFunction();
} catch (Exception $ex) {
echo $ex->getMessage();
} // try/catch
?>
Output: "My Message".
:-)