The SplAutoloader Interface
Overview
While any valid PHP callback may be registered with spl_autoload_register(), Zend Framework autoloaders often provide more flexibility by being stateful and allowing configuration. To provide a common interface, Zend Framework provides the SplAutoloader interface.
Objects implementing this interface provide a standard mechanism for configuration, a method that may be invoked to attempt to load a class, and a method for registering with the SPL autoloading mechanism.
Quick Start
To create your own autoloading mechanism, simply create a class implementing the SplAutoloader interface (you may review the methods defined in the Methods section). As a simple example, consider the following autoloader, which will look for a class file named after the class within a list of registered directories.
- require_once 'Zend/Loader/SplAutoloader.php';
- class Custom_ModifiedIncludePathAutoloader implements Zend_Loader_SplAutoloader
- {
- public function __construct($options = null)
- {
- if (null !== $options) {
- $this->setOptions($options);
- }
- }
- public function setOptions($options)
- {
- throw new InvalidArgumentException();
- }
- foreach ($options as $path) {
- $this->paths[] = $path;
- }
- }
- return $this;
- }
- public function autoload($classname)
- {
- $filename = $classname . '.php';
- foreach ($this->paths as $path) {
- $test = $path . DIRECTORY_SEPARATOR . $filename;
- return include($test);
- }
- }
- return false;
- }
- public function register()
- {
- }
- }
Configuration Options
This component defines no configuration options, as it is an interface.
Available Methods
Examples
Please see the Quick Start for a complete example.