note 73561 added to language.oop5.autoload

From: Date: Thu, 01 Mar 2007 15:35:22 +0000
Subject: note 73561 added to language.oop5.autoload
Groups: php.notes 
Request: Send a blank email to php-notes+get-123982@lists.php.net to get a copy of this message
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". :-) ---- Server IP: 69.147.83.197 Probable Submitter: 84.163.250.193 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php Edit -- https://master.php.net/note/edit/73561 Del: integrated -- https://master.php.net/note/delete/73561/integrated Del: useless -- https://master.php.net/note/delete/73561/useless Del: bad code -- https://master.php.net/note/delete/73561/bad+code Del: spam -- https://master.php.net/note/delete/73561/spam Del: non-english -- https://master.php.net/note/delete/73561/non-english Del: in docs -- https://master.php.net/note/delete/73561/in+docs Del: other reasons-- https://master.php.net/note/delete/73561 Reject -- https://master.php.net/note/reject/73561 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#123982) next »