autoloading-usage.xml 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.autoloading.usage">
  4. <title>Basic Autoloader Usage</title>
  5. <para>
  6. Now that we have an understanding of what autoloading is and the goals and design of Zend
  7. Framework's autoloading solution, let's look at how to use
  8. <classname>Zend_Loader_Autoloader</classname>.
  9. </para>
  10. <para>
  11. In the simplest case, you would simply require the class, and then instantiate it. Since
  12. <classname>Zend_Loader_Autoloader</classname> is a singleton (due to the fact that the SPL
  13. autoloader is a single resource), we use <methodname>getInstance()</methodname> to retrieve
  14. an instance.
  15. </para>
  16. <programlisting language="php"><![CDATA[
  17. require_once 'Zend/Loader/Autoloader.php';
  18. Zend_Loader_Autoloader::getInstance();
  19. ]]></programlisting>
  20. <para>
  21. By default, this will allow loading any classes with the class namespace prefixes of "Zend_"
  22. or "ZendX_", as long as they are on your <constant>include_path</constant>.
  23. </para>
  24. <para>
  25. What happens if you have other namespace prefixes you wish to use? The best, and simplest,
  26. way is to call the <methodname>registerNamespace()</methodname> method on the instance. You
  27. can pass a single namespace prefix, or an array of them:
  28. </para>
  29. <programlisting language="php"><![CDATA[
  30. require_once 'Zend/Loader/Autoloader.php';
  31. $loader = Zend_Loader_Autoloader::getInstance();
  32. $loader->registerNamespace('Foo_');
  33. $loader->registerNamespace(array('Foo_', 'Bar_'));
  34. ]]></programlisting>
  35. <para>
  36. Alternately, you can tell <classname>Zend_Loader_Autoloader</classname> to act as a
  37. "fallback" autoloader. This means that it will try to resolve any class regardless of
  38. namespace prefix.
  39. </para>
  40. <programlisting language="php"><![CDATA[
  41. $loader->setFallbackAutoloader(true);
  42. ]]></programlisting>
  43. <warning>
  44. <title>Do not use as a fallback autoloader</title>
  45. <para>
  46. While it's tempting to use <classname>Zend_Loader_Autoloader</classname> as a fallback
  47. autoloader, we do not recommend the practice.
  48. </para>
  49. <para>
  50. Internally, <classname>Zend_Loader_Autoloader</classname> uses
  51. <methodname>Zend_Loader::loadClass()</methodname> to load classes. That method uses
  52. <methodname>include()</methodname> to attempt to load the given class file.
  53. <methodname>include()</methodname> will return a boolean <constant>false</constant>
  54. if not successful -- but also issues a PHP warning. This latter fact can lead to some
  55. issues:
  56. </para>
  57. <itemizedlist>
  58. <listitem>
  59. <para>
  60. If <constant>display_errors</constant> is enabled, the warning will be included
  61. in output.
  62. </para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. Depending on the <constant>error_reporting</constant> level you have chosen, it
  67. could also clutter your logs.
  68. </para>
  69. </listitem>
  70. </itemizedlist>
  71. <para>
  72. You can suppress the error messages (the <classname>Zend_Loader_Autoloader</classname>
  73. documentation details this), but note that the suppression is only relevant when
  74. <constant>display_errors</constant> is enabled; the error log will always display the
  75. messages. For these reasons, we recommend always configuring the namespace prefixes the
  76. autoloader should be aware of
  77. </para>
  78. </warning>
  79. <note>
  80. <title>Namespace Prefixes vs PHP Namespaces</title>
  81. <para>
  82. At the time this is written, PHP 5.3 has been released. With that version, PHP now has
  83. official namespace support.
  84. </para>
  85. <para>
  86. However, Zend Framework predates PHP 5.3, and thus namespaces. Within Zend Framework,
  87. when we refer to "namespaces", we are referring to a practice whereby classes are
  88. prefixed with a vender "namespace". As an example, all Zend Framework class names are
  89. prefixed with "Zend_" -- that is our vendor "namespace".
  90. </para>
  91. <para>
  92. Zend Framework plans to offer native PHP namespace support to the autoloader in future
  93. revisions, and its own library will utilize namespaces starting with version 2.0.0.
  94. </para>
  95. </note>
  96. <para>
  97. If you have a custom autoloader you wish to use with Zend Framework -- perhaps an autoloader
  98. from a third-party library you are also using -- you can manage it with
  99. <classname>Zend_Loader_Autoloader</classname>'s <methodname>pushAutoloader()</methodname>
  100. and <methodname>unshiftAutoloader()</methodname> methods. These methods will append or
  101. prepend, respectively, autoloaders to a chain that is called prior to executing ZF's
  102. internal autoloading mechanism. This approach offers the following benefits:
  103. </para>
  104. <itemizedlist>
  105. <listitem>
  106. <para>
  107. Each method takes an optional second argument, a class namespace prefix. This can be
  108. used to indicate that the given autoloader should only be used when looking up
  109. classes with that given class prefix. If the class being resolved does not have that
  110. prefix, the autoloader will be skipped -- which can lead to performance
  111. improvements.
  112. </para>
  113. </listitem>
  114. <listitem>
  115. <para>
  116. If you need to manipulate <methodname>spl_autoload</methodname>'s registry, any
  117. autoloaders that are callbacks pointing to instance methods can pose issues, as
  118. <methodname>spl_autoload_functions()</methodname> does not return the exact same
  119. callbacks. <classname>Zend_Loader_Autoloader</classname> has no such limitation.
  120. </para>
  121. </listitem>
  122. </itemizedlist>
  123. <para>
  124. Autoloaders managed this way may be any valid PHP callback.
  125. </para>
  126. <programlisting language="php"><![CDATA[
  127. // Append function 'my_autoloader' to the stack,
  128. // to manage classes with the prefix 'My_':
  129. $loader->pushAutoloader('my_autoloader', 'My_');
  130. // Prepend static method Foo_Loader::autoload() to the stack,
  131. // to manage classes with the prefix 'Foo_':
  132. $loader->unshiftAutoloader(array('Foo_Loader', 'autoload'), 'Foo_');
  133. ]]></programlisting>
  134. </sect1>