note 85161 added to function.spl-autoload-register

From: Date: Mon, 18 Aug 2008 13:41:02 +0000
Subject: note 85161 added to function.spl-autoload-register
Groups: php.notes 
Request: Send a blank email to php-notes+get-143586@lists.php.net to get a copy of this message
This is my version of making it possible to throw exceptions within autoload functions. I liked the functions which used __construct and __callStatic to throw the exceptions, but I did not want to relay on __callStatic because it only exists since PHP version 5.3.0. I tested all three examples below in PHP version 5.2.6. The class named Test and Test_Interface did both not exists. <?php new Test(); Test::test(); class Test implements Test_Interface {} ?> In all three cases it succesfully throwed the exception without any fatal errors. <?php function __autoload($sClass) { try { throw new Exception('It works'); } catch (Exception $oException) { throwAutoloadException($sClass, $oException); } } function throwAutoloadException($sClass, Exception $oException) { // The use of eval is risky, because it could be easily exploited. To prefend // that the use of eval in this method could be exploited we make sure that the // variable contains only the allowed characters of a class name. if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $sClass)) { eval('class '.$sClass.' { static public function throwException() { throw unserialize(\''.serialize($oException).'\'); } } '.$sClass.'::throwException();'); } } ?> ---- Server IP: 89.248.170.61 Probable Submitter: 217.149.205.247 ---- Manual Page -- http://www.php.net/manual/en/function.spl-autoload-register.php Edit -- https://master.php.net/note/edit/85161 Del: integrated -- https://master.php.net/note/delete/85161/integrated Del: useless -- https://master.php.net/note/delete/85161/useless Del: bad code -- https://master.php.net/note/delete/85161/bad+code Del: spam -- https://master.php.net/note/delete/85161/spam Del: non-english -- https://master.php.net/note/delete/85161/non-english Del: in docs -- https://master.php.net/note/delete/85161/in+docs Del: other reasons-- https://master.php.net/note/delete/85161 Reject -- https://master.php.net/note/reject/85161 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#143586) next »