Zend_Loader-SplAutoloader.xml 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. <variablelist>
  79. <varlistentry id="zend.loader.spl-autoloader.methods.constructor">
  80. <term>
  81. <methodsynopsis>
  82. <methodname>__construct</methodname>
  83. <methodparam>
  84. <funcparams>$options = null</funcparams>
  85. </methodparam>
  86. </methodsynopsis>
  87. </term>
  88. <listitem>
  89. <para>
  90. Initialize and configure an autoloader
  91. </para>
  92. <para>
  93. Autoloader constructors should optionally receive configuration options.
  94. Typically, if received, these will be passed to the
  95. <methodname>setOptions()</methodname> method to process.
  96. </para>
  97. </listitem>
  98. </varlistentry>
  99. <varlistentry id="zend.loader.spl-autoloader.methods.set-options">
  100. <term>
  101. <methodsynopsis>
  102. <methodname>setOptions</methodname>
  103. <methodparam>
  104. <funcparams>$options</funcparams>
  105. </methodparam>
  106. </methodsynopsis>
  107. </term>
  108. <listitem>
  109. <para>
  110. Configure the autoloader state
  111. </para>
  112. <para>
  113. Used to configure the autoloader. Typically, it should expect either an array or
  114. a <interfacename>Traversable</interfacename> object, though validation of the
  115. options is left to implementation. Additionally, it is recommended that the
  116. method return the autoloader instance in order to implement a fluent interface.
  117. </para>
  118. </listitem>
  119. </varlistentry>
  120. <varlistentry id="zend.loader.spl-autoloader.methods.autoload">
  121. <term>
  122. <methodsynopsis>
  123. <methodname>autoload</methodname>
  124. <methodparam>
  125. <funcparams>$classname</funcparams>
  126. </methodparam>
  127. </methodsynopsis>
  128. </term>
  129. <listitem>
  130. <para>
  131. Attempt to resolve a class name to the file defining it
  132. </para>
  133. <para>
  134. This method should be used to resolve a class name to the file defining it. When
  135. a positive match is found, return the class name; otherwise, return a boolean
  136. false.
  137. </para>
  138. </listitem>
  139. </varlistentry>
  140. <varlistentry id="zend.loader.spl-autoloader.methods.register">
  141. <term>
  142. <methodsynopsis>
  143. <methodname>register</methodname>
  144. </methodsynopsis>
  145. </term>
  146. <listitem>
  147. <para>
  148. Register the autoloader with the SPL autoloader
  149. </para>
  150. <para>
  151. Should be used to register the autoloader instance with
  152. <function>spl_autoload_register()</function>. Invariably, the method
  153. should look like the following:
  154. </para>
  155. <programlisting language="php"><![CDATA[
  156. public function register()
  157. {
  158. spl_autoload_register(array($this, 'autoload'));
  159. }
  160. ]]></programlisting>
  161. </listitem>
  162. </varlistentry>
  163. </variablelist>
  164. </sect2>
  165. <sect2 id="zend.loader.spl-autoloader.examples">
  166. <title>Examples</title>
  167. <para>
  168. Please see the <link linkend="zend.loader.spl-autoloader.quick-start">Quick Start</link>
  169. for a complete example.
  170. </para>
  171. </sect2>
  172. </sect1>