Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.9.5
-
Fix Version/s: 1.9.6
-
Component/s: Zend_Loader
-
Labels:None
Description
Currently (r19141 of the standard trunk), the resource autoloader can produce some unexpected PHP warnings if it accidentally attempts to autoload something that doesn't exist. E.g.:
1. Create and configure an instance of the resource autoloader; for example:
require_once 'Zend/Loader/Autoloader.php';
require_once 'Zend/Loader/Autoloader/Resource.php';
$autoloader = new Zend_Loader_Autoloader_Resource(array(
'namespace' => 'My',
'basePath' => '/path/to/my',
));$autoloader->addResourceType('resource', 'resources', 'Resource');
2. Attempt to instantiate a class that doesn't exist, but would be in the configured namespace:
$obj = new My_Resource_DoesNotExist();
At this point you'll get two PHP errors. The first is an E_WARNING message indicating that you tried to include a file that doesn't exist; the second is an E_FATAL message indicating that you tried to instantiate a class that doesn't exist.
However, if the behavior of Zend_Loader_Autoloader::autoload() is any indicator, Zend_Loader_Autoloader_Resource::autoload() should instead return false if the expected class cannot be found. This will get rid of the first E_WARNING message and enable a bit more flexible error handling.
For an example of why this would be helpful, please take a look at http://www.doctrine-project.org/jira/browse/DC-137 : Doctrine has a feature that will generate a certain class on the fly if it can't be autoloaded; if the current ZF resource autoloader is registered, this will still work correctly ...but not without generating the E_WARNING message mentioned earlier.
Thanks!
Adam
On further inspection, I realized it's not the return value of autoload() that's causing this ...in fact, it's returning false for classes-not-found, just as documented.
However, I do think it would be worth avoiding the E_WARNING messages. Developers will still get enough error information out of the E_FATAL message that follows it when a non-existing class is being instantiated ...and in the meantime, other situations (like Doctrine's use of class_exists()) would still function gracefully without emitting any errors.
I should be attaching a patch shortly; if this sounds like a good idea, it'd be a pretty easy fix.
Thanks!
Adam