autoloading-usage.xml 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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
  13. <acronym>SPL</acronym> autoloader is a single resource), we use
  14. <methodname>getInstance()</methodname> to retrieve 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 <property>include_path</property>.
  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 <acronym>PHP</acronym> warning. This latter
  55. fact can lead to some issues:
  56. </para>
  57. <itemizedlist>
  58. <listitem>
  59. <para>
  60. If <property>display_errors</property> is enabled, the warning will be included
  61. in output.
  62. </para>
  63. </listitem>
  64. <listitem>
  65. <para>
  66. Depending on the <property>error_reporting</property> 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. <property>display_errors</property> 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, <acronym>PHP</acronym> 5.3 has been released. With that
  83. version, <acronym>PHP</acronym> now has official namespace support.
  84. </para>
  85. <para>
  86. However, Zend Framework predates <acronym>PHP</acronym> 5.3, and thus namespaces.
  87. Within Zend Framework, when we refer to "namespaces", we are referring to a practice
  88. whereby classes are prefixed with a vender "namespace". As an example, all Zend
  89. Framework class names are prefixed with "Zend_" -- that is our vendor "namespace".
  90. </para>
  91. <para>
  92. Zend Framework plans to offer native <acronym>PHP</acronym> namespace support to the
  93. autoloader in future revisions, and its own library will utilize namespaces starting
  94. with version 2.0.0.
  95. </para>
  96. </note>
  97. <para>
  98. If you have a custom autoloader you wish to use with Zend Framework -- perhaps an autoloader
  99. from a third-party library you are also using -- you can manage it with
  100. <classname>Zend_Loader_Autoloader</classname>'s <methodname>pushAutoloader()</methodname>
  101. and <methodname>unshiftAutoloader()</methodname> methods. These methods will append or
  102. prepend, respectively, autoloaders to a chain that is called prior to executing Zend
  103. Framework's internal autoloading mechanism. This approach offers the following benefits:
  104. </para>
  105. <itemizedlist>
  106. <listitem>
  107. <para>
  108. Each method takes an optional second argument, a class namespace prefix. This can be
  109. used to indicate that the given autoloader should only be used when looking up
  110. classes with that given class prefix. If the class being resolved does not have that
  111. prefix, the autoloader will be skipped -- which can lead to performance
  112. improvements.
  113. </para>
  114. </listitem>
  115. <listitem>
  116. <para>
  117. If you need to manipulate <methodname>spl_autoload()</methodname>'s registry, any
  118. autoloaders that are callbacks pointing to instance methods can pose issues, as
  119. <methodname>spl_autoload_functions()</methodname> does not return the exact same
  120. callbacks. <classname>Zend_Loader_Autoloader</classname> has no such limitation.
  121. </para>
  122. </listitem>
  123. </itemizedlist>
  124. <para>
  125. Autoloaders managed this way may be any valid <acronym>PHP</acronym> callback.
  126. </para>
  127. <programlisting language="php"><![CDATA[
  128. // Append function 'my_autoloader' to the stack,
  129. // to manage classes with the prefix 'My_':
  130. $loader->pushAutoloader('my_autoloader', 'My_');
  131. // Prepend static method Foo_Loader::autoload() to the stack,
  132. // to manage classes with the prefix 'Foo_':
  133. $loader->unshiftAutoloader(array('Foo_Loader', 'autoload'), 'Foo_');
  134. ]]></programlisting>
  135. </sect1>