Zend_Loader_Autoloader
introduces a comprehensive
autoloading solution for Zend Framework. It has been designed with
several goals in mind:
Provide a true namespace autoloader. (Previous incarnations intercepted all userland namespaces.)
Allow registering arbitrary callbacks as autoloaders, and manage
them as a stack. (At the time of this writing, this overcomes some
issues with spl_autoload
, which does not allow
re-registering a callback that utilizes an instance method.)
Allow optimistic matching of namespaces to provide faster class resolution.
Zend_Loader_Autoloader
implements a singleton, making it
unversally accessible. This provides the ability to register additional
autoloaders from anywhere in your code as necessary.
The first time an instance of the autoloader is retrieved, it
registers itself with spl_autoload
. You retrieve an
instance using the getInstance()
method:
$autoloader = Zend_Loader_Autoloader::getInstance();
By default, the autoloader is configured to match the "Zend_" and
"ZendX_" namespaces. If you have your own library code that uses
your own namespace, you may register it with the autoloader using
the registerNamespace()
method. For instance, if your
library code is prefixed with "My_", you could do so as follows:
$autoloader->registerNamespace('My_');
![]() |
Namespace Prefixes |
---|---|
You'll note that the previous example uses "My_" and not "My".
This is because |
You can also register arbitrary autoloader callbacks, optionally
with a specific namespace (or group of namespaces).
Zend_Loader_Autoloader
will attempt to match these
first before using its internal autoloading mechanism.
As an example, you may want to utilize one or more eZcomponents
components with your Zend Framework application. To use its
autoloading capabilities, push it onto the autoloader stack using
pushAutoloader()
:
$autoloader->pushAutoloader(array('ezcBase', 'autoload'), 'ezc');
This tells the autoloader to use the eZcomponents autoloader for classes beginning with "ezc".
You can use the unshiftAutoloader()
method to add the
autoloader to the beginning of the autoloader chain.
By default, Zend_Loader_Autoloader
does no error
suppression when using its internal autoloader, which utilizes
Zend_Loader::loadClass()
. Most of the time, this is
exactly what you want. However, there may be cases where you want to
suppress them. You can do this using
suppressNotFoundWarnings()
:
$autoloader->suppressNotFoundWarnings(true);
Finally, there may be times when you want the autoloader to load any
namespace. For instance, PEAR libraries do not share a common
namespace, making specifying individual namespaces difficult when
many PEAR components are in use. You can use the
setFallbackAutoloader()
method to have the autoloader
act as a catch-all:
$autoloader->setFallbackAutoloader(true);
Besides being able to specify arbitrary callbacks as autoloaders,
Zend Framework also defines an interface autoloading classes may
imlement, Zend_Loader_Autoloader_Interface
:
interface Zend_Loader_Autoloader_Interface { public function autoload($class); }
When using this interface, you can simply pass a class instance to
Zend_Loader_Autoloader
's pushAutoloader()
and unshiftAutoloader()
methods:
// Assume Foo_Autoloader implements Zend_Loader_Autoloader_Interface: $foo = new Foo_Autoloader(); $autoloader->pushAutoloader($foo, 'Foo_');
Below, please find a guide to the methods available in
Zend_Loader_Autoloader
.
表 30.1. Zend_Loader_Autoloader Methods
Method | Return Value | Parameters | Description |
---|---|---|---|
getInstance() |
Zend_Loader_Autoloader |
N/A |
Retrieve the |
resetInstance() |
void |
N/A |
Resets the state of the |
autoload($class) |
string|false |
|
Attempt to resolve a class name to a file and load it. |
setDefaultAutoloader($callback) |
Zend_Loader_Autoloader |
|
Specify an alternate PHP callback to use for the default autoloader implementation. |
getDefaultAutoloader() |
callback |
N/A |
Retrieve the default autoloader implementation; by
default, this is
|
setAutoloaders(array $autoloaders) |
Zend_Loader_Autoloader |
|
Set a list of concrete autoloaders to use in the autoloader stack. Each item in the autoloaders array must be a PHP callback. |
getAutoloaders() |
Array | N/A | Retrieve the internal autoloader stack. |
getNamespaceAutoloaders($namespace) |
Array |
|
Fetch all autoloaders that have registered to load a specific namespace. |
registerNamespace($namespace) |
Zend_Loader_Autoloader |
|
Register one or more namespaces with the default
autoloader. If |
unregisterNamespace($namespace) |
Zend_Loader_Autoloader |
|
Unregister one or more namespaces from the default
autoloader. If |
getRegisteredNamespace() |
Array | N/A | Returns an array of all namespaces registered with the default autoloader. |
suppressNotFoundWarnings($flag = null) |
boolean|Zend_Loader_Autoloader |
|
Set or retrieve the value of the flag used to indicate whether the default autoloader implementation should suppress "file not found" warnings. If no arguments or a null value is passed, returns a boolean indicating the status of the flag; if a boolean is passed, the flag is set to that value and the autoloader instance is returned (to allow method chaining). |
setFallbackAutoloader($flag) |
Zend_Loader_Autoloader |
|
Set the value of the flag used to indicate whether or not the default autoloader should be used as a fallback or catch-all autoloader for all namespaces. |
isFallbackAutoloader() |
Boolean | N/A | Retrieve the value of the flag used to indicate whether or not the default autoloader should be used as a fallback or catch-all autoloader for all namespaces. By default, this is false. |
getClassAutoloaders($class) |
Array |
|
Get the list of namespaced autoloaders that could potentially match the provided class. If none match, all global (non-namespaced) autoloaders are returned. |
unshiftAutoloader($callback, $namespace = '') |
Zend_Loader_Autoloader |
|
Add a concrete autoloader implementation to the beginning of the internal autoloader stack. If a namespace is provided, that namespace will be used to match optimistically; otherwise, the autoloader will be considered a global autoloader. |
pushAutoloader($callback, $namespace = '') |
Zend_Loader_Autoloader |
|
Add a concrete autoloader implementation to the end of the internal autoloader stack. If a namespace is provided, that namespace will be used to match optimistically; otherwise, the autoloader will be considered a global autoloader. |
removeAutoloader($callback, $namespace = '') |
Zend_Loader_Autoloader |
|
Remove a concrete autoloader implementation from the internal autoloader stack. If a namespace or namespaces are provided, the callback will be removed from that namespace or namespaces only. |