1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.loader.autoloader">
4 <title>The Autoloader</title>
7 <classname>Zend_Loader_Autoloader</classname> introduces a comprehensive
8 autoloading solution for Zend Framework. It has been designed with
15 Provide a true namespace autoloader. (Previous incarnations
16 intercepted all userland namespaces.)
22 Allow registering arbitrary callbacks as autoloaders, and manage
23 them as a stack. (At the time of this writing, this overcomes some
24 issues with <code>spl_autoload</code>, which does not allow
25 re-registering a callback that utilizes an instance method.)
31 Allow optimistic matching of namespaces to provide faster class resolution.
37 <classname>Zend_Loader_Autoloader</classname> implements a singleton, making it
38 unversally accessible. This provides the ability to register additional
39 autoloaders from anywhere in your code as necessary.
42 <sect2 id="zend.loader.autoloader.usage">
43 <title>Using the Autoloader</title>
46 The first time an instance of the autoloader is retrieved, it
47 registers itself with <code>spl_autoload</code>. You retrieve an
48 instance using the <methodname>getInstance()</methodname> method:
51 <programlisting language="php"><![CDATA[
52 $autoloader = Zend_Loader_Autoloader::getInstance();
56 By default, the autoloader is configured to match the "Zend_" and
57 "ZendX_" namespaces. If you have your own library code that uses
58 your own namespace, you may register it with the autoloader using
59 the <methodname>registerNamespace()</methodname> method. For instance, if your
60 library code is prefixed with "My_", you could do so as follows:
63 <programlisting language="php"><![CDATA[
64 $autoloader->registerNamespace('My_');
68 <title>Namespace Prefixes</title>
71 You'll note that the previous example uses "My_" and not "My".
72 This is because <classname>Zend_Loader_Autoloader</classname> is intended
73 as a general purpose autoloader, and does not make the
74 assumption that a given class prefix namespace includes an
75 underscore. If your class namespace <emphasis>does</emphasis>
76 include one, you should include it when registering your
82 You can also register arbitrary autoloader callbacks, optionally
83 with a specific namespace (or group of namespaces).
84 <classname>Zend_Loader_Autoloader</classname> will attempt to match these
85 first before using its internal autoloading mechanism.
89 As an example, you may want to utilize one or more eZcomponents
90 components with your Zend Framework application. To use its
91 autoloading capabilities, push it onto the autoloader stack using
92 <methodname>pushAutoloader()</methodname>:
95 <programlisting language="php"><![CDATA[
96 $autoloader->pushAutoloader(array('ezcBase', 'autoload'), 'ezc');
100 This tells the autoloader to use the eZcomponents autoloader for
101 classes beginning with "ezc".
105 You can use the <methodname>unshiftAutoloader()</methodname> method to add the
106 autoloader to the beginning of the autoloader chain.
110 By default, <classname>Zend_Loader_Autoloader</classname> does no error
111 suppression when using its internal autoloader, which utilizes
112 <methodname>Zend_Loader::loadClass()</methodname>. Most of the time, this is
113 exactly what you want. However, there may be cases where you want to
114 suppress them. You can do this using
115 <methodname>suppressNotFoundWarnings()</methodname>:
118 <programlisting language="php"><![CDATA[
119 $autoloader->suppressNotFoundWarnings(true);
123 Finally, there may be times when you want the autoloader to load any
124 namespace. For instance, PEAR libraries do not share a common
125 namespace, making specifying individual namespaces difficult when
126 many PEAR components are in use. You can use the
127 <methodname>setFallbackAutoloader()</methodname> method to have the autoloader
131 <programlisting language="php"><![CDATA[
132 $autoloader->setFallbackAutoloader(true);
136 <title>Loading Classes from PHP Namespaces</title>
139 Starting in version 1.10.0, Zend Framework now allows loading classes from
140 <acronym>PHP</acronym> namespaces. This support follows the same guidelines and
141 implementation as that found in the <ulink
142 url="http://groups.google.com/group/php-standards/web/psr-0-final-proposal">PHP
143 Framework Interop Group PSR-0</ulink> reference implementation.
147 Under this guideline, the following rules apply:
153 Each namespace separator is converted to a
154 <constant>DIRECTORY_SEPARATOR</constant> when loading from the file system.
160 Each "_" character in the <emphasis>CLASS NAME</emphasis> is converted to a
161 <constant>DIRECTORY_SEPARATOR</constant>. The "_" character has no special
162 meaning in the namespace.
168 The fully-qualified namespace and class is suffixed with ".php" when loading
169 from the file system.
181 <classname>\Doctrine\Common\IsolatedClassLoader</classname> =>
182 <filename>/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php</filename>
188 <classname>\namespace\package\Class_Name</classname> =>
189 <filename>/path/to/project/lib/vendor/namespace/package/Class/Name.php</filename>
195 <classname>\namespace\package_name\Class_Name</classname> =>
196 <filename>/path/to/project/lib/vendor/namespace/package_name/Class/Name.php</filename>
203 <sect2 id="zend.loader.autoloader.zf-version">
204 <title>Selecting a Zend Framework version</title>
207 Typically, you will use the version of Zend Framework that the autoloader you
208 instantiate came with. However, when developing a project, it's often useful to track
209 specific versions, major or minor branches, or just the latest version.
210 <classname>Zend_Loader_Autoloader</classname>, as of version 1.10, offers some features
211 to help manage this task.
215 Imagine the following scenario:
221 During <emphasis>development</emphasis>, you want to track the latest version of
222 Zend Framework you have installed, so that you can ensure the application works
223 when you upgrade between versions.
227 When pushing to <emphasis>Quality Assurance</emphasis>, however, you need to
228 have slightly more stability, so you want to use the latest installed revision
229 of a specific minor version.
233 Finally, when you push to <emphasis>production</emphasis>, you want to pin to a
234 specific installed version, to ensure no breakage occurs if or when you add new
235 versions of Zend Framework to you server.
241 The autoloader allows you to do this with the method
242 <methodname>setZfPath()</methodname>. This method takes two arguments, a
243 <emphasis>path</emphasis> to a set of Zend Framework installations, and a
244 <emphasis>version</emphasis> to use. Once invoked, it prepends a path to the
245 <constant>include_path</constant> pointing to the appropriate Zend Framework
246 installation library.
250 The directory you specify as your <emphasis>path</emphasis> should have a tree such as
254 <programlisting language="text"><![CDATA[
258 |-- ZendFramework-1.9.1-minimal/
264 |-- ZendFramework-1.8.3/
275 (where <emphasis>path</emphasis> points to the directory "ZendFramework" in the above
280 Note that each subdirectory should contain the directory <filename>library</filename>,
281 which contains the actual Zend Framework library code. The individual subdirectory names
282 may be version numbers, or simply be the untarred contents of a standard Zend Framework
283 distribution tarball/zipfile.
287 Now, let's address the use cases. In the first use case, in
288 <emphasis>development</emphasis>, we want to track the latest source install. We can do
289 that by passing "latest" as the version:
292 <programlisting language="php"><![CDATA[
293 $autoloader->setZfPath($path, 'latest');
297 In the example from above, this will map to the directory
298 <filename>ZendFramework/1.9.2/library/</filename>; you can verify this by checking the
299 return value of <methodname>getZfPath()</methodname>.
303 In the second situation, for <emphasis>quality assurance</emphasis>, let's say we want
304 to pin to the 1.8 minor release, using the latest install you have for that release. You
305 can do so as follows:
308 <programlisting language="php"><![CDATA[
309 $autoloader->setZfPath($path, '1.8');
313 In this case, it will find the directory
314 <filename>ZendFramework/1.8.4PL1/library/</filename>.
318 In the final case, for <emphasis>production</emphasis>, we'll pin to a specific version
319 -- 1.7.7, since that was what was available when Quality Assurance tested prior to our
323 <programlisting language="php"><![CDATA[
324 $autoloader->setZfPath($path, '1.7.7');
328 Predictably, it finds the directory <filename>ZendFramework/1.7.7/library/</filename>.
332 You can also specify these values in the configuration file you use with
333 <filename>Zend_Application</filename>. To do so, you'd specify the following
337 <programlisting language="ini"><![CDATA[
339 autoloaderZfPath = "path/to/ZendFramework"
340 autoloaderZfVersion = "1.7.7"
343 autoloaderZfVersion = "1.8"
346 autoloaderZfVersion = "latest"
350 Note the different environment sections, and the different version specified in each
351 environment; these factors will allow <classname>Zend_Application</classname> to
352 configure the autoloader appropriately.
356 <title>Performance implications</title>
359 For best performance, either do not use this feature, or specify a specific Zend
360 Framework version (i.e., not "latest", a major revision such as "1", or a minor
361 revision such as "1.8"). Otherwise, the autoloader will need to scan the provided
362 path for directories matching the criteria -- a somewhat expensive operation to
363 perform on each request.
368 <sect2 id="zend.loader.autoloader.interface">
369 <title>The Autoloader Interface</title>
372 Besides being able to specify arbitrary callbacks as autoloaders,
373 Zend Framework also defines an interface autoloading classes may
374 imlement, <classname>Zend_Loader_Autoloader_Interface</classname>:
377 <programlisting language="php"><![CDATA[
378 interface Zend_Loader_Autoloader_Interface
380 public function autoload($class);
385 When using this interface, you can simply pass a class instance to
386 <classname>Zend_Loader_Autoloader</classname>'s
387 <methodname>pushAutoloader()</methodname>
388 and <methodname>unshiftAutoloader()</methodname> methods:
391 <programlisting language="php"><![CDATA[
392 // Assume Foo_Autoloader implements Zend_Loader_Autoloader_Interface:
393 $foo = new Foo_Autoloader();
395 $autoloader->pushAutoloader($foo, 'Foo_');
399 <sect2 id="zend.loader.autoloader.reference">
400 <title>Autoloader Reference</title>
403 Below, please find a guide to the methods available in
404 <classname>Zend_Loader_Autoloader</classname>.
407 <table id="zend.loader.autoloader.reference.api">
408 <title>Zend_Loader_Autoloader Methods</title>
413 <entry>Method</entry>
414 <entry>Return Value</entry>
415 <entry>Parameters</entry>
416 <entry>Description</entry>
422 <entry><methodname>getInstance()</methodname></entry>
423 <entry><classname>Zend_Loader_Autoloader</classname></entry>
428 Retrieve the <classname>Zend_Loader_Autoloader</classname>
429 singleton instance. On first retrieval, it registers
430 itself with <code>spl_autoload</code>. This method is static.
436 <entry><methodname>resetInstance()</methodname></entry>
437 <entry><code>void</code></entry>
442 Resets the state of the
443 <classname>Zend_Loader_Autoloader</classname> singleton instance to
444 it's original state, unregistering all autoloader callbacks and all
445 registered namespaces.
451 <entry><methodname>autoload($class)</methodname></entry>
452 <entry><code>string|<constant>FALSE</constant></code></entry>
458 <varname>$class</varname>, <emphasis>required</emphasis>.
459 A string class name to load.
466 <para>Attempt to resolve a class name to a file and load it.</para>
471 <entry><methodname>setDefaultAutoloader($callback)</methodname></entry>
472 <entry><classname>Zend_Loader_Autoloader</classname></entry>
478 <varname>$callback</varname>, <emphasis>required</emphasis>.
486 Specify an alternate <acronym>PHP</acronym> callback to use for the
487 default autoloader implementation.
493 <entry><methodname>getDefaultAutoloader()</methodname></entry>
494 <entry><code>callback</code></entry>
499 Retrieve the default autoloader implementation; by default, this is
500 <methodname>Zend_Loader::loadClass()</methodname>.
506 <entry><methodname>setAutoloaders(array $autoloaders)</methodname></entry>
507 <entry><classname>Zend_Loader_Autoloader</classname></entry>
513 <varname>$autoloaders</varname>,
514 <emphasis>required</emphasis>.
522 Set a list of concrete autoloaders to use in the
523 autoloader stack. Each item in the autoloaders array
524 must be a <acronym>PHP</acronym> callback.
530 <entry><methodname>getAutoloaders()</methodname></entry>
531 <entry><type>Array</type></entry>
533 <entry><para>Retrieve the internal autoloader stack.</para></entry>
537 <entry><methodname>getNamespaceAutoloaders($namespace)</methodname></entry>
538 <entry><type>Array</type></entry>
544 <varname>$namespace</varname>, <emphasis>required</emphasis>
552 Fetch all autoloaders that have registered to load a
559 <entry><methodname>registerNamespace($namespace)</methodname></entry>
560 <entry><classname>Zend_Loader_Autoloader</classname></entry>
566 <varname>$namespace</varname>,
567 <emphasis>required</emphasis>.
575 Register one or more namespaces with the default
576 autoloader. If <varname>$namespace</varname> is a string,
577 it registers that namespace; if it's an array of
578 strings, registers each as a namespace.
584 <entry><methodname>unregisterNamespace($namespace)</methodname></entry>
585 <entry><classname>Zend_Loader_Autoloader</classname></entry>
591 <varname>$namespace</varname>,
592 <emphasis>required</emphasis>.
600 Unregister one or more namespaces from the default
601 autoloader. If <varname>$namespace</varname> is a string,
602 it unregisters that namespace; if it's an array of
603 strings, unregisters each as a namespace.
609 <entry><methodname>getRegisteredNamespaces()</methodname></entry>
610 <entry><type>Array</type></entry>
615 Returns an array of all namespaces registered with the default
623 <methodname>suppressNotFoundWarnings($flag = null)</methodname>
626 <entry><code>boolean|Zend_Loader_Autoloader</code></entry>
632 <varname>$flag</varname>, <emphasis>optional</emphasis>.
640 Set or retrieve the value of the flag used to
641 indicate whether the default autoloader
642 implementation should suppress "file not found"
643 warnings. If no arguments or a <constant>NULL</constant> value is
644 passed, returns a boolean indicating the status of the flag;
645 if a boolean is passed, the flag is set to that
646 value and the autoloader instance is returned (to
647 allow method chaining).
653 <entry><methodname>setFallbackAutoloader($flag)</methodname></entry>
654 <entry><classname>Zend_Loader_Autoloader</classname></entry>
660 <varname>$flag</varname>, <emphasis>required</emphasis>.
668 Set the value of the flag used to indicate whether
669 or not the default autoloader should be used as a
670 fallback or catch-all autoloader for all namespaces.
676 <entry><methodname>isFallbackAutoloader()</methodname></entry>
677 <entry><type>Boolean</type></entry>
681 Retrieve the value of the flag used to indicate whether
682 or not the default autoloader should be used as a
683 fallback or catch-all autoloader for all namespaces.
684 By default, this is <constant>FALSE</constant>.
690 <entry><methodname>getClassAutoloaders($class)</methodname></entry>
691 <entry><type>Array</type></entry>
697 <varname>$class</varname>, <emphasis>required</emphasis>.
705 Get the list of namespaced autoloaders that could
706 potentially match the provided class. If none match,
707 all global (non-namespaced) autoloaders are returned.
714 <methodname>unshiftAutoloader($callback, $namespace = '')</methodname>
717 <entry><classname>Zend_Loader_Autoloader</classname></entry>
723 <varname>$callback</varname>, <emphasis>required</emphasis>.
724 A valid <acronym>PHP</acronym> callback
730 <varname>$namespace</varname>,
731 <emphasis>optional</emphasis>. A string representing a class
740 Add a concrete autoloader implementation to the
741 beginning of the internal autoloader stack. If a
742 namespace is provided, that namespace will be used
743 to match optimistically; otherwise, the autoloader
744 will be considered a global autoloader.
751 <methodname>pushAutoloader($callback, $namespace = '')</methodname>
753 <entry><classname>Zend_Loader_Autoloader</classname></entry>
759 <varname>$callback</varname>, <emphasis>required</emphasis>.
760 A valid <acronym>PHP</acronym> callback
766 <varname>$namespace</varname>,
767 <emphasis>optional</emphasis>. A string representing a class
776 Add a concrete autoloader implementation to the
777 end of the internal autoloader stack. If a
778 namespace is provided, that namespace will be used
779 to match optimistically; otherwise, the autoloader
780 will be considered a global autoloader.
787 <methodname>removeAutoloader($callback, $namespace = '')</methodname>
790 <entry><classname>Zend_Loader_Autoloader</classname></entry>
796 <varname>$callback</varname>, <emphasis>required</emphasis>.
797 A valid <acronym>PHP</acronym> callback
803 <varname>$namespace</varname>,
804 <emphasis>optional</emphasis>. A string representing a class
805 prefix namespace, or an array of namespace strings.
813 Remove a concrete autoloader implementation from
814 the internal autoloader stack. If a namespace or
815 namespaces are provided, the callback will be
816 removed from that namespace or namespaces only.