Description

Autoload functions should not cause errors when they can't include the requested class. They may only be one function in the autoload stack, or they may be being used by class_exists() to simply check if a class is available. It isn't the job of any individual autoload to ensure a class is defined, therefore they should fail silently in the event that they cannot include the class.

At present the Zend_Loader::loadClass() method (used by Zend_Loader::autoload()) causes a PHP file not found warning if it cannot include the requested class. (Line 83)

Comments

Posted by Darby Felton (darby) on 2008-03-19T10:00:48.000+0000

One obvious solution is to change the {{autoload()}} method to suppress errors on the call to {{self::loadClass()}}, but what happens when the file does exist, and it has a parse error or a warning that the developer should see? Such error suppression solves one problem while causing other potential problems. The complexity of dealing with PHP errors here is a bit like a waterbed... if you push down one side, the other side pops up.

The next step would be to capture the errors and make them available to the calling script (except fatal errors of course), but this may be a performance hog. We'll need some tests and benchmarks, I think.

Maybe someone else has a better idea?

Posted by Jack Sleight (jacksleight) on 2008-03-19T10:33:39.000+0000

So we have four options:

Don't check the file exists before inclusion and get errors when not found

Don't check the file exists before inclusion and suppress all errors, including parse errors

Don't check the file exists before inclusion but somehow catch the PHP file not found warning and suppress it, at the cost of performance

Check the file exists before inclusion, at the cost of performance

None of them are ideal really. For the time being I've settled on option 4 for my workaround. Although I've got no idea on the actual performance impact of this.

I've just run some (fairly unscientific) benchmarks on my standard application skeleton, here's the results:

Average load time in seconds based on 10 requests:

Using simple error suppression on the include_once (@): 0.3055
Checking file exists before inclusion (Zend_Loader::isReadable()): 0.3442
Using the new error suppression method in incubator: 0.3086

So, there's practically no performance hit with the new method. Looks good to me.

You make a good point regarding the inclusion of other files, but I see that as less of an issue. I guess you could always change the regex in suppressFileNotFoundWarnings() to look for the file name as well, so that only errors related to that file are suppressed? Mind you, you can't parse anything to the error handler can you? So I guess it would complicated things further.

Posted by old of Satoru Yoshida (yoshida@zend.co.jp) on 2008-04-23T10:55:00.000+0000