Zend_Loader-SplAutoloader.xml 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.loader.spl-autoloader">
  4. <title>The SplAutoloader Interface</title>
  5. <sect2 id="zend.loader.spl-autoloader.intro">
  6. <title>Overview</title>
  7. <para>
  8. While any valid PHP callback may be registered with
  9. <function>spl_autoload_register()</function>, Zend Framework autoloaders often provide
  10. more flexibility by being stateful and allowing configuration. To provide a common
  11. interface, Zend Framework provides the <interfacename>SplAutoloader</interfacename>
  12. interface.
  13. </para>
  14. <para>
  15. Objects implementing this interface provide a standard mechanism for configuration, a
  16. method that may be invoked to attempt to load a class, and a method for registering with
  17. the SPL autoloading mechanism.
  18. </para>
  19. </sect2>
  20. <sect2 id="zend.loader.spl-autoloader.quick-start">
  21. <title>Quick Start</title>
  22. <para>
  23. To create your own autoloading mechanism, simply create a class implementing the
  24. <interfacename>SplAutoloader</interfacename> interface (you may review the methods
  25. defined in the <link linkend="zend.loader.spl-autoloader.methods">Methods
  26. section</link>). As a simple example, consider the following autoloader, which will look
  27. for a class file named after the class within a list of registered directories.
  28. </para>
  29. <programlisting language="php"><![CDATA[
  30. require_once 'Zend/Loader/SplAutoloader.php';
  31. class Custom_ModifiedIncludePathAutoloader implements Zend_Loader_SplAutoloader
  32. {
  33. protected $paths = array();
  34. public function __construct($options = null)
  35. {
  36. if (null !== $options) {
  37. $this->setOptions($options);
  38. }
  39. }
  40. public function setOptions($options)
  41. {
  42. if (!is_array($options) && !($options instanceof Traversable)) {
  43. throw new InvalidArgumentException();
  44. }
  45. foreach ($options as $path) {
  46. if (!in_array($path, $this->paths)) {
  47. $this->paths[] = $path;
  48. }
  49. }
  50. return $this;
  51. }
  52. public function autoload($classname)
  53. {
  54. $filename = $classname . '.php';
  55. foreach ($this->paths as $path) {
  56. $test = $path . DIRECTORY_SEPARATOR . $filename;
  57. if (file_exists($test)) {
  58. return include($test);
  59. }
  60. }
  61. return false;
  62. }
  63. public function register()
  64. {
  65. spl_autoload_register(array($this, 'autoload'));
  66. }
  67. }
  68. ]]></programlisting>
  69. </sect2>
  70. <sect2 id="zend.loader.spl-autoloader.options">
  71. <title>Configuration Options</title>
  72. <para>
  73. This component defines no configuration options, as it is an interface.
  74. </para>
  75. </sect2>
  76. <sect2 id="zend.loader.spl-autoloader.methods">
  77. <title>Available Methods</title>
  78. <refentry id="zend.loader.spl-autoloader.methods.constructor">
  79. <refnamediv>
  80. <refname>__construct</refname>
  81. <refpurpose>Initialize and configure an autoloader</refpurpose>
  82. </refnamediv>
  83. <refsynopsisdiv>
  84. <methodsynopsis>
  85. <methodname>__construct</methodname>
  86. <methodparam>
  87. <funcparams>$options = null</funcparams>
  88. </methodparam>
  89. </methodsynopsis>
  90. </refsynopsisdiv>
  91. <refsection>
  92. <title>Constructor</title>
  93. <para>
  94. Autoloader constructors should optionally receive configuration options.
  95. Typically, if received, these will be passed to the
  96. <methodname>setOptions()</methodname> method to process.
  97. </para>
  98. </refsection>
  99. </refentry>
  100. <refentry id="zend.loader.spl-autoloader.methods.set-options">
  101. <refnamediv>
  102. <refname>setOptions</refname>
  103. <refpurpose>Configure the autoloader state</refpurpose>
  104. </refnamediv>
  105. <refsynopsisdiv>
  106. <methodsynopsis>
  107. <methodname>setOptions</methodname>
  108. <methodparam>
  109. <funcparams>$options</funcparams>
  110. </methodparam>
  111. </methodsynopsis>
  112. </refsynopsisdiv>
  113. <refsection>
  114. <title>setOptions()</title>
  115. <para>
  116. Used to configure the autoloader. Typically, it should expect either an array or
  117. a <interfacename>Traversable</interfacename> object, though validation of the
  118. options is left to implementation. Additionally, it is recommended that the
  119. method return the autoloader instance in order to implement a fluent interface.
  120. </para>
  121. </refsection>
  122. </refentry>
  123. <refentry id="zend.loader.spl-autoloader.methods.autoload">
  124. <refnamediv>
  125. <refname>autoload</refname>
  126. <refpurpose>Attempt to resolve a class name to the file defining it</refpurpose>
  127. </refnamediv>
  128. <refsynopsisdiv>
  129. <methodsynopsis>
  130. <methodname>autoload</methodname>
  131. <methodparam>
  132. <funcparams>$classname</funcparams>
  133. </methodparam>
  134. </methodsynopsis>
  135. </refsynopsisdiv>
  136. <refsection>
  137. <title>autoload()</title>
  138. <para>
  139. This method should be used to resolve a class name to the file defining it. When
  140. a positive match is found, return the class name; otherwise, return a boolean
  141. false.
  142. </para>
  143. </refsection>
  144. </refentry>
  145. <refentry id="zend.loader.spl-autoloader.methods.register">
  146. <refnamediv>
  147. <refname>register</refname>
  148. <refpurpose>Register the autoloader with the SPL autoloader</refpurpose>
  149. </refnamediv>
  150. <refsynopsisdiv>
  151. <methodsynopsis>
  152. <methodname>register</methodname>
  153. </methodsynopsis>
  154. </refsynopsisdiv>
  155. <refsection>
  156. <title>register()</title>
  157. <para>
  158. Should be used to register the autoloader instance with
  159. <function>spl_autoload_register()</function>. Invariably, the method
  160. should look like the following:
  161. </para>
  162. <programlisting language="php"><![CDATA[
  163. public function register()
  164. {
  165. spl_autoload_register(array($this, 'autoload'));
  166. }
  167. ]]></programlisting>
  168. </refsection>
  169. </refentry>
  170. </sect2>
  171. <sect2 id="zend.loader.spl-autoloader.examples">
  172. <title>Examples</title>
  173. <para>
  174. Please see the <link linkend="zend.loader.spl-autoloader.quick-start">Quick Start</link>
  175. for a complete example.
  176. </para>
  177. </sect2>
  178. </sect1>