note 107504 added to function.class-exists

From: Date: Tue, 14 Feb 2012 06:47:13 +0000
Subject: note 107504 added to function.class-exists
Groups: php.notes 
Request: Send a blank email to php-notes+get-186116@lists.php.net to get a copy of this message
I'm running PHP 5.3.4 on Windows 7 and had some difficulty autoloading classes using class_exists(). In my case, when I checked for the class and it didn't exist, class_exists automatically threw a system Exception. I was also throwing my own exception resulting in an uncaught exception. <?php /** * Set my include path here */ $include_path = array( '/include/this/dir', '/include/this/one/too' ); set_include_path( $include_path ); spl_autoload_register(); /** * Assuming I have my own custom exception handler (MyException) let's * try to see if a file exists. */ try { if( ! file_exists( 'myfile.php' ) ) { throw new MyException('Doh!'); } include( 'myfile.php' ); } catch( MyException $e ) { echo $e->getMessage(); } /** * The above code either includes myfile.php or throws the new MyException * as expected. No problem right? The same should be true of class_exists(), * right? So then... */ $classname = 'NonExistentClass'; try { if( ! class_exists( $classname ) ) { throw new MyException('Double Doh!'); } $var = new $classname(); } catch( MyException $e ) { echo $e->getMessage(); } /** * Should throw a new instance of MyException. But instead I get an * uncaught LogicException blah blah blah for the default Exception * class AND MyException. I only catch MyException so we've got on * uncaught resulting in the dreaded LogicException error. */ ?> By registering an additional autoload handler function that did nothing, I was able to stop throwing the extra Exception and only throw my own. <?php /** * Set my include path here */ $include_path = array( '/include/this/dir', '/include/this/one/too' ); set_include_path( $include_path ); spl_autoload_register(); spl_autoload_register( 'myAutoLoad' ); // Add these two and no worries... function myAutoLoad() {} /** * By registering the additional custom autoload function that does nothing * class_exists() returns only boolean and does NOT throw an uncaught Exception */ ?> Found this buried in some search results. I don't remember the page URL but if it would have been here it might have saved me some time! ---- Server IP: 69.147.83.197 Probable Submitter: 216.47.194.136 ---- Manual Page -- http://www.php.net/manual/en/function.class-exists.php Edit -- https://master.php.net/note/edit/107504 Del: integrated -- https://master.php.net/note/delete/107504/integrated Del: useless -- https://master.php.net/note/delete/107504/useless Del: bad code -- https://master.php.net/note/delete/107504/bad+code Del: spam -- https://master.php.net/note/delete/107504/spam Del: non-english -- https://master.php.net/note/delete/107504/non-english Del: in docs -- https://master.php.net/note/delete/107504/in+docs Del: other reasons-- https://master.php.net/note/delete/107504 Reject -- https://master.php.net/note/reject/107504 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#186116) next »