note 76224 deleted from language.oop5.autoload by felipe
| From: | felipe@php.net | Date: | Mon, 24 Mar 2008 22:41:47 +0000 |
| Subject: | note 76224 deleted from language.oop5.autoload by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-137504@lists.php.net to get a copy of this message | ||
Note Submitter: Krzysztof 'ChanibaL' Bociurko
----
It should be noted that after you throw the exception using the fake evaled class, __autoload() will
NOT run anymore with this class, because of the already existing fake one, so the second time you
try new UndefinedClass() there will be NO exception, however if you throw an Exception in the
__construct() of the fake class it will be cought.
Example code:
<?php
function __autoload($class) {
if(file_exists($f="{$class}.php"))
require_once($f);
else {
$errorText="Could not find class {$class}, searched in {$f}";
eval("class {$class} { "
."public function __construct() {throw new
ClassNotFoundException('$errorText');}"
.'};');
throw new ClassNotFoundException($errorText);
}
}
?>
Unfortunately this still doesn't work 100% correct, because it relies on class initialization,
therefore static sets/gets/calls will produce a fatal error if the unexisting class was being tried
to be accessed before, as in:
<?php
try {
new Undefined();
}
catch (ClassNotFoundException $e) {
// the problematic fake class Undefined should exist by now...
}
Undefined::$staticVar='some value';
print Undefined::$staticVar;
print Undefined::staticMethod();
print Undefined::SOME_CONST;
?>
Overloading the __set(), __get() or __call() won't help, they don't work with statics.