简体   繁体   中英

PHP __autoload() with exception handling - waste of time?

I'm a bit new to OOP and I'm working on a 'framework' for my own application. I have my own autoload function which looks like below.. as well as an exception handling object. I won't be using any 3rd party plugins (at least I don't think).

First question: Should I bother with exception handling in my autoload or am I just overdoing things?

Second question: My exceptionHandler class is a public function...since it's something that will be used by many other applications is this the right? Thank you.

Thanks..any input is greatly appreciated.

function __autoload( $class ){ // Define filename pattern to include $filename = $_SERVER['DOCUMENT_ROOT'] . '/../app/core/models/' . $class . '.class.php'; // Require class if it exists try { if ( is_readable( $filename ) ) { require_once ( $filename ); } else { throw new Exception( "Class filename doesn't exist or isn't named correctly: $filename" ); } } catch ( Exception $e ) { // Send to exceptionHandler Class for logging/handling. $err = new exceptionHandler( $e, 3 ); } } 

Any autoload function is really not meant to throw exceptions, because it's not clear where that exception should rise and you would have to either wrap the entire code in a try catch block or define a exception handler.

You should trigger an error via trigger_error or even better let PHP tells you what's the class that is not able to load in the real line where it happens (therefore just ignoring files that doesn't exists).

Also you shouldn't use __autoload but spl_autoload_register .

Regarding your first question: No, you shouldn't.

First reason: You should always be able to chain multiple autoloaders (see spl_autoload_register ), so if one autoloader fails, a second one might be able to load that class. Because of this, your autoloader should not throw any errors unless you know exactly what you're doing.

Second reason: If all autoloading attempts fail, php will trigger an error anyway. Log your php errors properly and you won't need to log this in your autoloader.

Third reason: Your autoloader will be a piece of code that will be executed a lot . You don't want a try/catch block here.

Apart from that: What happens, if autoloading your exceptionHandler fails...?

Thank you for the feedback Jeffrey and Alexander. You guys got me thinking.. I ended up removing my home grown autoloader for an easier (and faster?) method...

In my config file I added the include path for my core application classes as well as the module classes:

set_include_path( get_include_path() . PATH_SEPARATOR . APPLICATION_ROOT . 'core' . DIRECTORY_SEPARATOR . PATH_SEPARATOR . APPLICATION_ROOT . 'module' . DIRECTORY_SEPARATOR );
spl_autoload_extensions( '.class.php' );
spl_autoload_register();

So far it works :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM