note 80369 added to language.oop5.autoload

From: Date: Sat, 12 Jan 2008 05:31:07 +0000
Subject: note 80369 added to language.oop5.autoload
Groups: php.notes 
Request: Send a blank email to php-notes+get-135051@lists.php.net to get a copy of this message
You can allow __autoload() to throw exceptions without resulting in fatal errors by trying class_exists(), literally. <?php function __autoload($class) { if (file_exists($file = "./inc/$class.php")) { include($file); } else { throw new Exception("Class $class not found"); } } // By 'literally', I mean this try { // Since the second argument defaults to true, __autoload() // will be called from this function class_exists('MyClass'); } catch (Exception $e) { // Catch the exception and handle it as usual die($e->getMessage()); } // Safely initialize an object from the class $class = new MyClass(); ?> ---- Server IP: 203.211.135.122 Probable Submitter: 218.186.14.10 (proxied: 218.212.16.27) ---- Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php Edit -- https://master.php.net/note/edit/80369 Del: integrated -- https://master.php.net/note/delete/80369/integrated Del: useless -- https://master.php.net/note/delete/80369/useless Del: bad code -- https://master.php.net/note/delete/80369/bad+code Del: spam -- https://master.php.net/note/delete/80369/spam Del: non-english -- https://master.php.net/note/delete/80369/non-english Del: in docs -- https://master.php.net/note/delete/80369/in+docs Del: other reasons-- https://master.php.net/note/delete/80369 Reject -- https://master.php.net/note/reject/80369 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#135051) next »