autoloading-usage.xml 6.7 KB

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