2
0

Zend_Loader-Autoloader.xml 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.loader.autoloader">
  4. <title>The Autoloader</title>
  5. <para>
  6. <classname>Zend_Loader_Autoloader</classname> introduces a comprehensive
  7. autoloading solution for Zend Framework. It has been designed with
  8. several goals in mind:
  9. </para>
  10. <itemizedlist>
  11. <listitem><para>
  12. Provide a true namespace autoloader. (Previous incarnations
  13. intercepted all userland namespaces.)
  14. </para></listitem>
  15. <listitem><para>
  16. Allow registering arbitrary callbacks as autoloaders, and manage
  17. them as a stack. (At the time of this writing, this overcomes some
  18. issues with <code>spl_autoload</code>, which does not allow
  19. re-registering a callback that utilizes an instance method.)
  20. </para></listitem>
  21. <listitem><para>
  22. Allow optimistic matching of namespaces to provide faster class
  23. resolution.
  24. </para></listitem>
  25. </itemizedlist>
  26. <para>
  27. <classname>Zend_Loader_Autoloader</classname> implements a singleton, making it
  28. unversally accessible. This provides the ability to register additional
  29. autoloaders from anywhere in your code as necessary.
  30. </para>
  31. <sect2 id="zend.loader.autoloader.usage">
  32. <title>Using the Autoloader</title>
  33. <para>
  34. The first time an instance of the autoloader is retrieved, it
  35. registers itself with <code>spl_autoload</code>. You retrieve an
  36. instance using the <code>getInstance()</code> method:
  37. </para>
  38. <programlisting language="php"><![CDATA[
  39. $autoloader = Zend_Loader_Autoloader::getInstance();
  40. ]]></programlisting>
  41. <para>
  42. By default, the autoloader is configured to match the "Zend_" and
  43. "ZendX_" namespaces. If you have your own library code that uses
  44. your own namespace, you may register it with the autoloader using
  45. the <code>registerNamespace()</code> method. For instance, if your
  46. library code is prefixed with "My_", you could do so as follows:
  47. </para>
  48. <programlisting language="php"><![CDATA[
  49. $autoloader->registerNamespace('My_');
  50. ]]></programlisting>
  51. <note>
  52. <title>Namespace Prefixes</title>
  53. <para>
  54. You'll note that the previous example uses "My_" and not "My".
  55. This is because <classname>Zend_Loader_Autoloader</classname> is intended
  56. as a general purpose autoloader, and does not make the
  57. assumption that a given class prefix namespace includes an
  58. underscore. If your class namespace <emphasis>does</emphasis>
  59. include one, you should include it when registering your
  60. namespace.
  61. </para>
  62. </note>
  63. <para>
  64. You can also register arbitrary autoloader callbacks, optionally
  65. with a specific namespace (or group of namespaces).
  66. <classname>Zend_Loader_Autoloader</classname> will attempt to match these
  67. first before using its internal autoloading mechanism.
  68. </para>
  69. <para>
  70. As an example, you may want to utilize one or more eZcomponents
  71. components with your Zend Framework application. To use its
  72. autoloading capabilities, push it onto the autoloader stack using
  73. <code>pushAutoloader()</code>:
  74. </para>
  75. <programlisting language="php"><![CDATA[
  76. $autoloader->pushAutoloader(array('ezcBase', 'autoload'), 'ezc');
  77. ]]></programlisting>
  78. <para>
  79. This tells the autoloader to use the eZcomponents autoloader for
  80. classes beginning with "ezc".
  81. </para>
  82. <para>
  83. You can use the <code>unshiftAutoloader()</code> method to add the
  84. autoloader to the beginning of the autoloader chain.
  85. </para>
  86. <para>
  87. By default, <classname>Zend_Loader_Autoloader</classname> does no error
  88. suppression when using its internal autoloader, which utilizes
  89. <classname>Zend_Loader::loadClass()</classname>. Most of the time, this is
  90. exactly what you want. However, there may be cases where you want to
  91. suppress them. You can do this using
  92. <code>suppressNotFoundWarnings()</code>:
  93. </para>
  94. <programlisting language="php"><![CDATA[
  95. $autoloader->suppressNotFoundWarnings(true);
  96. ]]></programlisting>
  97. <para>
  98. Finally, there may be times when you want the autoloader to load any
  99. namespace. For instance, PEAR libraries do not share a common
  100. namespace, making specifying individual namespaces difficult when
  101. many PEAR components are in use. You can use the
  102. <code>setFallbackAutoloader()</code> method to have the autoloader
  103. act as a catch-all:
  104. </para>
  105. <programlisting language="php"><![CDATA[
  106. $autoloader->setFallbackAutoloader(true);
  107. ]]></programlisting>
  108. </sect2>
  109. <sect2 id="zend.loader.autoloader.interface">
  110. <title>The Autoloader Interface</title>
  111. <para>
  112. Besides being able to specify arbitrary callbacks as autoloaders,
  113. Zend Framework also defines an interface autoloading classes may
  114. imlement, <classname>Zend_Loader_Autoloader_Interface</classname>:
  115. </para>
  116. <programlisting language="php"><![CDATA[
  117. interface Zend_Loader_Autoloader_Interface
  118. {
  119. public function autoload($class);
  120. }
  121. ]]></programlisting>
  122. <para>
  123. When using this interface, you can simply pass a class instance to
  124. <classname>Zend_Loader_Autoloader</classname>'s <code>pushAutoloader()</code>
  125. and <code>unshiftAutoloader()</code> methods:
  126. </para>
  127. <programlisting language="php"><![CDATA[
  128. // Assume Foo_Autoloader implements Zend_Loader_Autoloader_Interface:
  129. $foo = new Foo_Autoloader();
  130. $autoloader->pushAutoloader($foo, 'Foo_');
  131. ]]></programlisting>
  132. </sect2>
  133. <sect2 id="zend.loader.autoloader.reference">
  134. <title>Autoloader Reference</title>
  135. <para>
  136. Below, please find a guide to the methods available in
  137. <classname>Zend_Loader_Autoloader</classname>.
  138. </para>
  139. <table id="zend.loader.autoloader.reference.api">
  140. <title>Zend_Loader_Autoloader Methods</title>
  141. <tgroup cols="4">
  142. <thead>
  143. <row>
  144. <entry>Method</entry>
  145. <entry>Return Value</entry>
  146. <entry>Parameters</entry>
  147. <entry>Description</entry>
  148. </row>
  149. </thead>
  150. <tbody>
  151. <row>
  152. <entry><code>getInstance()</code></entry>
  153. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  154. <entry>N/A</entry>
  155. <entry><para>
  156. Retrieve the <classname>Zend_Loader_Autoloader</classname>
  157. singleton instance. On first retrieval, it registers
  158. itself with <code>spl_autoload</code>. This method
  159. is static.
  160. </para></entry>
  161. </row>
  162. <row>
  163. <entry><code>resetInstance()</code></entry>
  164. <entry><code>void</code></entry>
  165. <entry>N/A</entry>
  166. <entry><para>
  167. Resets the state of the <classname>Zend_Loader_Autoloader</classname>
  168. singleton instance to it's original state,
  169. unregistering all autoloader callbacks and all
  170. registered namespaces.
  171. </para></entry>
  172. </row>
  173. <row>
  174. <entry><code>autoload($class)</code></entry>
  175. <entry><code>string|false</code></entry>
  176. <entry><itemizedlist>
  177. <listitem><para>
  178. <code>$class</code>, <emphasis>required</emphasis>.
  179. A string class name to load.
  180. </para></listitem>
  181. </itemizedlist></entry>
  182. <entry><para>
  183. Attempt to resolve a class name to a file and load
  184. it.
  185. </para></entry>
  186. </row>
  187. <row>
  188. <entry><code>setDefaultAutoloader($callback)</code></entry>
  189. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  190. <entry><itemizedlist>
  191. <listitem><para>
  192. <code>$callback</code>, <emphasis>required</emphasis>.
  193. </para></listitem>
  194. </itemizedlist></entry>
  195. <entry><para>
  196. Specify an alternate PHP callback to use for the
  197. default autoloader implementation.
  198. </para></entry>
  199. </row>
  200. <row>
  201. <entry><code>getDefaultAutoloader()</code></entry>
  202. <entry><code>callback</code></entry>
  203. <entry>N/A</entry>
  204. <entry><para>
  205. Retrieve the default autoloader implementation; by
  206. default, this is
  207. <classname>Zend_Loader::loadClass()</classname>.
  208. </para></entry>
  209. </row>
  210. <row>
  211. <entry><code>setAutoloaders(array $autoloaders)</code></entry>
  212. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  213. <entry><itemizedlist>
  214. <listitem><para>
  215. <code>$autoloaders</code>, <emphasis>required</emphasis>.
  216. </para></listitem>
  217. </itemizedlist></entry>
  218. <entry><para>
  219. Set a list of concrete autoloaders to use in the
  220. autoloader stack. Each item in the autoloaders array
  221. must be a PHP callback.
  222. </para></entry>
  223. </row>
  224. <row>
  225. <entry><code>getAutoloaders()</code></entry>
  226. <entry><type>Array</type></entry>
  227. <entry>N/A</entry>
  228. <entry><para>
  229. Retrieve the internal autoloader stack.
  230. </para></entry>
  231. </row>
  232. <row>
  233. <entry><code>getNamespaceAutoloaders($namespace)</code></entry>
  234. <entry><type>Array</type></entry>
  235. <entry><itemizedlist>
  236. <listitem><para>
  237. <code>$namespace</code>, <emphasis>required</emphasis>
  238. </para></listitem>
  239. </itemizedlist></entry>
  240. <entry><para>
  241. Fetch all autoloaders that have registered to load a
  242. specific namespace.
  243. </para></entry>
  244. </row>
  245. <row>
  246. <entry><code>registerNamespace($namespace)</code></entry>
  247. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  248. <entry><itemizedlist>
  249. <listitem><para>
  250. <code>$namespace</code>, <emphasis>required</emphasis>.
  251. </para></listitem>
  252. </itemizedlist></entry>
  253. <entry><para>
  254. Register one or more namespaces with the default
  255. autoloader. If <code>$namespace</code> is a string,
  256. it registers that namespace; if it's an array of
  257. strings, registers each as a namespace.
  258. </para></entry>
  259. </row>
  260. <row>
  261. <entry><code>unregisterNamespace($namespace)</code></entry>
  262. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  263. <entry><itemizedlist>
  264. <listitem><para>
  265. <code>$namespace</code>, <emphasis>required</emphasis>.
  266. </para></listitem>
  267. </itemizedlist></entry>
  268. <entry><para>
  269. Unregister one or more namespaces from the default
  270. autoloader. If <code>$namespace</code> is a string,
  271. it unregisters that namespace; if it's an array of
  272. strings, unregisters each as a namespace.
  273. </para></entry>
  274. </row>
  275. <row>
  276. <entry><code>getRegisteredNamespace()</code></entry>
  277. <entry><type>Array</type></entry>
  278. <entry>N/A</entry>
  279. <entry><para>
  280. Returns an array of all namespaces registered with
  281. the default autoloader.
  282. </para></entry>
  283. </row>
  284. <row>
  285. <entry><code>suppressNotFoundWarnings($flag = null)</code></entry>
  286. <entry><code>boolean|Zend_Loader_Autoloader</code></entry>
  287. <entry><itemizedlist>
  288. <listitem><para>
  289. <code>$flag</code>, <emphasis>optional</emphasis>.
  290. </para></listitem>
  291. </itemizedlist></entry>
  292. <entry><para>
  293. Set or retrieve the value of the flag used to
  294. indicate whether the default autoloader
  295. implementation should suppress "file not found"
  296. warnings. If no arguments or a null value is passed,
  297. returns a boolean indicating the status of the flag;
  298. if a boolean is passed, the flag is set to that
  299. value and the autoloader instance is returned (to
  300. allow method chaining).
  301. </para></entry>
  302. </row>
  303. <row>
  304. <entry><code>setFallbackAutoloader($flag)</code></entry>
  305. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  306. <entry><itemizedlist>
  307. <listitem><para>
  308. <code>$flag</code>, <emphasis>required</emphasis>.
  309. </para></listitem>
  310. </itemizedlist></entry>
  311. <entry><para>
  312. Set the value of the flag used to indicate whether
  313. or not the default autoloader should be used as a
  314. fallback or catch-all autoloader for all namespaces.
  315. </para></entry>
  316. </row>
  317. <row>
  318. <entry><code>isFallbackAutoloader()</code></entry>
  319. <entry><type>Boolean</type></entry>
  320. <entry>N/A</entry>
  321. <entry><para>
  322. Retrieve the value of the flag used to indicate whether
  323. or not the default autoloader should be used as a
  324. fallback or catch-all autoloader for all namespaces.
  325. By default, this is false.
  326. </para></entry>
  327. </row>
  328. <row>
  329. <entry><code>getClassAutoloaders($class)</code></entry>
  330. <entry><type>Array</type></entry>
  331. <entry><itemizedlist>
  332. <listitem><para>
  333. <code>$class</code>, <emphasis>required</emphasis>.
  334. </para></listitem>
  335. </itemizedlist></entry>
  336. <entry><para>
  337. Get the list of namespaced autoloaders that could
  338. potentially match the provided class. If none match,
  339. all global (non-namespaced) autoloaders are
  340. returned.
  341. </para></entry>
  342. </row>
  343. <row>
  344. <entry><code>unshiftAutoloader($callback, $namespace = '')</code></entry>
  345. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  346. <entry><itemizedlist>
  347. <listitem><para>
  348. <code>$callback</code>, <emphasis>required</emphasis>.
  349. A valid PHP callback
  350. </para></listitem>
  351. <listitem><para>
  352. <code>$namespace</code>, <emphasis>optional</emphasis>.
  353. A string representing a class prefix namespace.
  354. </para></listitem>
  355. </itemizedlist></entry>
  356. <entry><para>
  357. Add a concrete autoloader implementation to the
  358. beginning of the internal autoloader stack. If a
  359. namespace is provided, that namespace will be used
  360. to match optimistically; otherwise, the autoloader
  361. will be considered a global autoloader.
  362. </para></entry>
  363. </row>
  364. <row>
  365. <entry><code>pushAutoloader($callback, $namespace = '')</code></entry>
  366. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  367. <entry><itemizedlist>
  368. <listitem><para>
  369. <code>$callback</code>, <emphasis>required</emphasis>.
  370. A valid PHP callback
  371. </para></listitem>
  372. <listitem><para>
  373. <code>$namespace</code>, <emphasis>optional</emphasis>.
  374. A string representing a class prefix namespace.
  375. </para></listitem>
  376. </itemizedlist></entry>
  377. <entry><para>
  378. Add a concrete autoloader implementation to the
  379. end of the internal autoloader stack. If a
  380. namespace is provided, that namespace will be used
  381. to match optimistically; otherwise, the autoloader
  382. will be considered a global autoloader.
  383. </para></entry>
  384. </row>
  385. <row>
  386. <entry><code>removeAutoloader($callback, $namespace = '')</code></entry>
  387. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  388. <entry><itemizedlist>
  389. <listitem><para>
  390. <code>$callback</code>, <emphasis>required</emphasis>.
  391. A valid PHP callback
  392. </para></listitem>
  393. <listitem><para>
  394. <code>$namespace</code>, <emphasis>optional</emphasis>.
  395. A string representing a class prefix namespace,
  396. or an array of namespace strings.
  397. </para></listitem>
  398. </itemizedlist></entry>
  399. <entry><para>
  400. Remove a concrete autoloader implementation from the
  401. the internal autoloader stack. If a namespace or
  402. namespaces are provided, the callback will be
  403. removed from that namespace or namespaces only.
  404. </para></entry>
  405. </row>
  406. </tbody>
  407. </tgroup>
  408. </table>
  409. </sect2>
  410. </sect1>