The ClassMapAutoloaderOverview
The ClassMapAutoloader is designed with performance in mind. The
idea behind it is simple: when asked to load a class, see if it's in the map, and, if
so, load the file associated with the class in the map. This avoids unnecessary
filesystem operations, and can also ensure the autoloader "plays nice" with opcode
caches and PHP's realpath cache.
In order to use the ClassMapAutoloader, you first need class
maps. Zend Framework also provides a tool for generating these class maps; you can find it in
bin/classmap_generator.php of the distribution. Full documentation
of this too is provided in .
Quick Start
The first step is to generate a class map file. You may run this over any directory
containing source code anywhere underneath it.
This will create a file named Some/Directory/autoload_classmap.php, which
is a PHP file returning an associative array that represents the class map.
Within your code, you will now instantiate the
ClassMapAutoloader, and provide it the location of the map.
registerAutoloadMap('Some/Directory/autoload_classmap.php');
// Register with spl_autoload:
$loader->register();
]]>
At this point, you may now use any classes referenced in your class map.
Configuration Options
The ClassMapAutoloader defines the following options.
ClassMapAutoloader Options$options
The ClassMapAutoloader expects an array of options,
where each option is either a filename referencing a class map, or an
associative array of class name/filename pairs.
As an example:
__DIR__ . '/application/Bootstrap.php',
'Test_Bootstrap' => __DIR__ . '/tests/Bootstrap.php',
),
);
]]>Available Methods__constructInitialize and configure the object__construct$options = nullConstructor
Used during instantiation of the object. Optionally, pass options, which may be
either an array or Traversable object; this
argument will be passed to setOptions().
setOptionsConfigure the autoloadersetOptions$optionssetOptions()
Configures the state of the autoloader, including registering class maps.
Expects an array or Traversable object; the
argument will be passed to registerAutoloadMaps().
registerAutoloadMapRegister a class mapregisterAutoloadMap$mapregisterAutoloadMap()
Registers a class map with the autoloader. $map may be either
a string referencing a PHP script that returns a class map, or an array defining
a class map.
More than one class map may be registered; each will be merged with the
previous, meaning it's possible for a later class map to overwrite entries from
a previously registered map.
registerAutoloadMapsRegister multiple class maps at onceregisterAutoloadMaps$mapsregisterAutoloadMaps()
Register multiple class maps with the autoloader. Expects either an array or
Traversable object; it then iterates over the
argument and passes each value to registerAutoloadMap().
getAutoloadMapRetrieve the current class mapgetAutoloadMapgetAutoloadMap()
Retrieves the state of the current class map; the return value is simply an
array.
autoloadAttempt to load a class.autoload$classautoload()
Attempts to load the class specified. Returns a boolean
false on failure, or a string indicating the class loaded
on success.
registerRegister with spl_autoload.registerregister()
Registers the autoload() method of the current instance
with spl_autoload_register().
ExamplesUsing configuration to seed ClassMapAutoloader
Often, you will want to configure your ClassMapAutoloader.
These values may come from a configuration file, a cache (such as ShMem or
memcached), or a simple PHP array. The following is an example of a PHP array that
could be used to configure the autoloader:
APPLICATION_PATH . '/Bootstrap.php',
'Test_Bootstrap' => APPLICATION_PATH . '/../tests/Bootstrap.php',
),
);
]]>
An eqivalent INI style configuration might look like this:
Once you have your configuration, you can pass it either to the constructor of the
ClassMapAutoloader, to its
setOptions() method, or to
registerAutoloadMaps().
setOptions($config);
// To registerAutoloadMaps():
$loader = new Zend_Loader_ClassMapAutoloader();
$loader->registerAutoloadMaps($config);
]]>