I’m building a custom autoloader based on Zend Framework’s autoloading (related question here).
The basic approach, taken from that question, is
class My_Autoloader implements Zend_Loader_Autoloader_Interface
{
public function autoload($class)
{
// add your logic to find the required classes in here
}
}
and then binding the new autoloader class to a class prefix.
Now what I’m unsure about is how to handle errors inside the autoload method (for example, “class file not found”) in a proper, ZF compliant way. I’m new to the framework, its conventions and style.
-
Do I quietly return false and let the class creation process crash?
-
Do I output an error or log message somehow (which would be nice to pinpoint the problem) and return false? If so, what is the Zend way of doing that?
-
Do I trigger an error?
-
Do I throw an exception? If so, what kind?
That depends on the kind of error. I’d consider it a fatal error if a class cannot be loaded. Thus I’d throw an Exception, e.g.
You will find that ZF uses a lot of custom Exceptions on the package level and also provides a class for this to extend from (though I’d consider this optional).
Incidentally, there is a usage example of
Zend_Exceptionwith their autoloader: