| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- <?xml version="1.0" encoding="utf-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.loader.class-map-autoloader">
- <title>The ClassMapAutoloader</title>
-
- <sect2 id="zend.loader.class-map-autoloader.intro">
- <title>Overview</title>
- <para>
- The <classname>ClassMapAutoloader</classname> 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.
- </para>
- <para>
- In order to use the <classname>ClassMapAutoloader</classname>, you first need class
- maps. Zend Framework also provides a tool for generating these class maps; you can find it in
- <filename>bin/classmap_generator.php</filename> of the distribution. Full documentation
- of this too is provided in <xref linkend="zend.loader.classmap-generator"/>.
- </para>
- </sect2>
- <sect2 id="zend.loader.class-map-autoloader.quick-start">
- <title>Quick Start</title>
- <para>
- The first step is to generate a class map file. You may run this over any directory
- containing source code anywhere underneath it.
- </para>
- <programlisting language="sh"><![CDATA[
- php classmap_generator.php Some/Directory/
- ]]></programlisting>
- <para>
- This will create a file named <filename>Some/Directory/autoload_classmap.php</filename>, which
- is a PHP file returning an associative array that represents the class map.
- </para>
- <para>
- Within your code, you will now instantiate the
- <classname>ClassMapAutoloader</classname>, and provide it the location of the map.
- </para>
- <programlisting language="php"><![CDATA[
- // This example assumes ZF is on your include_path.
- // You could also load the autoloader class from a path relative to the
- // current script, or via an absolute path.
- require_once 'Zend/Loader/ClassMapAutoloader.php';
- $loader = new Zend_Loader_ClassMapAutoloader();
- // Register the class map:
- $loader->registerAutoloadMap('Some/Directory/autoload_classmap.php');
- // Register with spl_autoload:
- $loader->register();
- ]]></programlisting>
- <para>
- At this point, you may now use any classes referenced in your class map.
- </para>
- </sect2>
- <sect2 id="zend.loader.class-map-autoloader.options">
- <title>Configuration Options</title>
- <para>
- The <classname>ClassMapAutoloader</classname> defines the following options.
- </para>
- <variablelist>
- <title>ClassMapAutoloader Options</title>
- <varlistentry>
- <term>$options</term>
- <listitem>
- <para>
- The <classname>ClassMapAutoloader</classname> 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.
- </para>
- <para>
- As an example:
- </para>
- <programlisting language="php"><![CDATA[
- // Configuration defining both a file-based class map, and an array map
- $config = array(
- __DIR__ . '/library/autoload_classmap.php', // file-based class map
- array( // array class map
- 'Application_Bootstrap' => __DIR__ . '/application/Bootstrap.php',
- 'Test_Bootstrap' => __DIR__ . '/tests/Bootstrap.php',
- ),
- );
- ]]></programlisting>
- </listitem>
- </varlistentry>
- </variablelist>
- </sect2>
- <sect2 id="zend.loader.class-map-autoloader.methods">
- <title>Available Methods</title>
- <refentry id="zend.loader.class-map-autoloader.methods.constructor">
- <refnamediv>
- <refname>__construct</refname>
- <refpurpose>Initialize and configure the object</refpurpose>
- </refnamediv>
- <refsynopsisdiv>
- <methodsynopsis>
- <methodname>__construct</methodname>
- <methodparam>
- <funcparams>$options = null</funcparams>
- </methodparam>
- </methodsynopsis>
- </refsynopsisdiv>
- <refsection>
- <title>Constructor</title>
- <para>
- Used during instantiation of the object. Optionally, pass options, which may be
- either an array or <interfacename>Traversable</interfacename> object; this
- argument will be passed to <link linkend="zend.loader.class-map-autoloader.methods.set-options">setOptions()</link>.
- </para>
- </refsection>
- </refentry>
- <refentry id="zend.loader.class-map-autoloader.methods.set-options">
- <refnamediv>
- <refname>setOptions</refname>
- <refpurpose>Configure the autoloader</refpurpose>
- </refnamediv>
- <refsynopsisdiv>
- <methodsynopsis>
- <methodname>setOptions</methodname>
- <methodparam>
- <funcparams>$options</funcparams>
- </methodparam>
- </methodsynopsis>
- </refsynopsisdiv>
- <refsection>
- <title>setOptions()</title>
- <para>
- Configures the state of the autoloader, including registering class maps.
- Expects an array or <interfacename>Traversable</interfacename> object; the
- argument will be passed to <link linkend="zend.loader.class-map-autoloader.methods.register-autoload-maps">registerAutoloadMaps()</link>.
- </para>
- </refsection>
- </refentry>
- <refentry id="zend.loader.class-map-autoloader.methods.register-autoload-map">
- <refnamediv>
- <refname>registerAutoloadMap</refname>
- <refpurpose>Register a class map</refpurpose>
- </refnamediv>
- <refsynopsisdiv>
- <methodsynopsis>
- <methodname>registerAutoloadMap</methodname>
- <methodparam>
- <funcparams>$map</funcparams>
- </methodparam>
- </methodsynopsis>
- </refsynopsisdiv>
- <refsection>
- <title>registerAutoloadMap()</title>
- <para>
- Registers a class map with the autoloader. <varname>$map</varname> may be either
- a string referencing a PHP script that returns a class map, or an array defining
- a class map.
- </para>
- <para>
- 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.
- </para>
- </refsection>
- </refentry>
- <refentry id="zend.loader.class-map-autoloader.methods.register-autoload-maps">
- <refnamediv>
- <refname>registerAutoloadMaps</refname>
- <refpurpose>Register multiple class maps at once</refpurpose>
- </refnamediv>
- <refsynopsisdiv>
- <methodsynopsis>
- <methodname>registerAutoloadMaps</methodname>
- <methodparam>
- <funcparams>$maps</funcparams>
- </methodparam>
- </methodsynopsis>
- </refsynopsisdiv>
- <refsection>
- <title>registerAutoloadMaps()</title>
- <para>
- Register multiple class maps with the autoloader. Expects either an array or
- <interfacename>Traversable</interfacename> object; it then iterates over the
- argument and passes each value to <link linkend="zend.loader.class-map-autoloader.methods.register-autoload-map">registerAutoloadMap()</link>.
- </para>
- </refsection>
- </refentry>
- <refentry id="zend.loader.class-map-autoloader.methods.get-autoload-map">
- <refnamediv>
- <refname>getAutoloadMap</refname>
- <refpurpose>Retrieve the current class map</refpurpose>
- </refnamediv>
- <refsynopsisdiv>
- <methodsynopsis>
- <methodname>getAutoloadMap</methodname>
- </methodsynopsis>
- </refsynopsisdiv>
- <refsection>
- <title>getAutoloadMap()</title>
- <para>
- Retrieves the state of the current class map; the return value is simply an
- array.
- </para>
- </refsection>
- </refentry>
- <refentry id="zend.loader.class-map-autoloader.methods.autoload">
- <refnamediv>
- <refname>autoload</refname>
- <refpurpose>Attempt to load a class.</refpurpose>
- </refnamediv>
- <refsynopsisdiv>
- <methodsynopsis>
- <methodname>autoload</methodname>
- <methodparam>
- <funcparams>$class</funcparams>
- </methodparam>
- </methodsynopsis>
- </refsynopsisdiv>
- <refsection>
- <title>autoload()</title>
- <para>
- Attempts to load the class specified. Returns a boolean
- <constant>false</constant> on failure, or a string indicating the class loaded
- on success.
- </para>
- </refsection>
- </refentry>
- <refentry id="zend.loader.class-map-autoloader.methods.register">
- <refnamediv>
- <refname>register</refname>
- <refpurpose>Register with spl_autoload.</refpurpose>
- </refnamediv>
- <refsynopsisdiv>
- <methodsynopsis>
- <methodname>register</methodname>
- </methodsynopsis>
- </refsynopsisdiv>
- <refsection>
- <title>register()</title>
- <para>
- Registers the <methodname>autoload()</methodname> method of the current instance
- with <function>spl_autoload_register()</function>.
- </para>
- </refsection>
- </refentry>
- </sect2>
- <sect2 id="zend.loader.class-map-autoloader.examples">
- <title>Examples</title>
- <example id="zend.loader.class-map-autoloader.examples.configuration">
- <title>Using configuration to seed ClassMapAutoloader</title>
- <para>
- Often, you will want to configure your <classname>ClassMapAutoloader</classname>.
- 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:
- </para>
- <programlisting language="php"><![CDATA[
- // Configuration defining both a file-based class map, and an array map
- $config = array(
- APPLICATION_PATH . '/../library/autoload_classmap.php', // file-based class map
- array( // array class map
- 'Application_Bootstrap' => APPLICATION_PATH . '/Bootstrap.php',
- 'Test_Bootstrap' => APPLICATION_PATH . '/../tests/Bootstrap.php',
- ),
- );
- ]]></programlisting>
-
- <para>
- An eqivalent INI style configuration might look like this:
- </para>
- <programlisting language="ini"><![CDATA[
- classmap.library = APPLICATION_PATH "/../library/autoload_classmap.php"
- classmap.resources.Application_Bootstrap = APPLICATION_PATH "/Bootstrap.php"
- classmap.resources.Test_Bootstrap = APPLICATION_PATH "/../tests/Bootstrap.php"
- ]]></programlisting>
- <para>
- Once you have your configuration, you can pass it either to the constructor of the
- <classname>ClassMapAutoloader</classname>, to its
- <methodname>setOptions()</methodname> method, or to
- <methodname>registerAutoloadMaps()</methodname>.
- </para>
- <programlisting language="php"><![CDATA[
- /* The following are all equivalent */
- // To the constructor:
- $loader = new Zend_Loader_ClassMapAutoloader($config);
- // To setOptions():
- $loader = new Zend_Loader_ClassMapAutoloader();
- $loader->setOptions($config);
- // To registerAutoloadMaps():
- $loader = new Zend_Loader_ClassMapAutoloader();
- $loader->registerAutoloadMaps($config);
- ]]></programlisting>
- </example>
- </sect2>
- </sect1>
|