note 76224 added to language.oop5.autoload

From: Date: Fri, 06 Jul 2007 05:28:39 +0000
Subject: note 76224 added to language.oop5.autoload
Groups: php.notes 
Request: Send a blank email to php-notes+get-128257@lists.php.net to get a copy of this message
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. ---- Server IP: 195.136.184.34 Probable Submitter: 83.15.56.98 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php Edit -- https://master.php.net/note/edit/76224 Del: integrated -- https://master.php.net/note/delete/76224/integrated Del: useless -- https://master.php.net/note/delete/76224/useless Del: bad code -- https://master.php.net/note/delete/76224/bad+code Del: spam -- https://master.php.net/note/delete/76224/spam Del: non-english -- https://master.php.net/note/delete/76224/non-english Del: in docs -- https://master.php.net/note/delete/76224/in+docs Del: other reasons-- https://master.php.net/note/delete/76224 Reject -- https://master.php.net/note/reject/76224 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128257) next »