Zend_Loader-Autoloader.xml 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 <methodname>getInstance()</methodname> 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 <methodname>registerNamespace()</methodname> 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. <methodname>pushAutoloader()</methodname>:
  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 <methodname>unshiftAutoloader()</methodname> 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. <methodname>Zend_Loader::loadClass()</methodname>. 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. <methodname>suppressNotFoundWarnings()</methodname>:
  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. <methodname>setFallbackAutoloader()</methodname> 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.zf-version">
  110. <title>Selecting a Zend Framework version</title>
  111. <para>
  112. Typically, you will use the version of Zend Framework that the autoloader you
  113. instantiate came with. However, when developing a project, it's often useful to track
  114. specific versions, major or minor branches, or just the latest version.
  115. <classname>Zend_Loader_Autoloader</classname>, as of version 1.10, offers some features
  116. to help manage this task.
  117. </para>
  118. <para>
  119. Imagine the following scenario:
  120. </para>
  121. <itemizedlist>
  122. <listitem>
  123. <para>
  124. During <emphasis>development</emphasis>, you want to track the latest version of
  125. Zend Framework you have installed, so that you can ensure the application works
  126. when you upgrade between versions.
  127. </para>
  128. <para>
  129. When pushing to <emphasis>Quality Assurance</emphasis>, however, you need to
  130. have slightly more stability, so you want to use the latest installed revision
  131. of a specific minor version.
  132. </para>
  133. <para>
  134. Finally, when you push to <emphasis>production</emphasis>, you want to pin to a
  135. specific installed version, to ensure no breakage occurs if or when you add new
  136. versions of Zend Framework to you server.
  137. </para>
  138. </listitem>
  139. </itemizedlist>
  140. <para>
  141. The autoloader allows you to do this with the method
  142. <methodname>setZfPath()</methodname>. This method takes two arguments, a
  143. <emphasis>path</emphasis> to a set of Zend Framework installations, and a
  144. <emphasis>version</emphasis> to use. Once invoked, it prepends a path to the
  145. <constant>include_path</constant> pointing to the appropriate Zend Framework
  146. installation library.
  147. </para>
  148. <para>
  149. The directory you specify as your <emphasis>path</emphasis> should have a tree such as
  150. the following:
  151. </para>
  152. <programlisting language="text"><![CDATA[
  153. ZendFramework/
  154. |-- 1.9.2/
  155. | |-- library/
  156. |-- ZendFramework-1.9.1-minimal/
  157. | |-- library/
  158. |-- 1.8.4PL1/
  159. | |-- library/
  160. |-- 1.8.4/
  161. | |-- library/
  162. |-- ZendFramework-1.8.3/
  163. | |-- library/
  164. |-- 1.7.8/
  165. | |-- library/
  166. |-- 1.7.7/
  167. | |-- library/
  168. |-- 1.7.6/
  169. | |-- library/
  170. ]]></programlisting>
  171. <para>
  172. (where <emphasis>path</emphasis> points to the directory "ZendFramework" in the above
  173. example)
  174. </para>
  175. <para>
  176. Note that each subdirectory should contain the directory <filename>library</filename>,
  177. which contains the actual Zend Framework library code. The individual subdirectory names
  178. may be version numbers, or simply be the untarred contents of a standard Zend Framework
  179. distribution tarball/zipfile.
  180. </para>
  181. <para>
  182. Now, let's address the use cases. In the first use case, in
  183. <emphasis>development</emphasis>, we want to track the latest source install. We can do
  184. that by passing "latest" as the version:
  185. </para>
  186. <programlisting language="php"><![CDATA[
  187. $autoloader->setZfPath($path, 'latest');
  188. ]]></programlisting>
  189. <para>
  190. In the example from above, this will map to the directory
  191. <filename>ZendFramework/1.9.2/library/</filename>; you can verify this by checking the
  192. return value of <methodname>getZfPath()</methodname>.
  193. </para>
  194. <para>
  195. In the second situation, for <emphasis>quality assurance</emphasis>, let's say we want
  196. to pin to the 1.8 minor release, using the latest install you have for that release. You
  197. can do so as follows:
  198. </para>
  199. <programlisting language="php"><![CDATA[
  200. $autoloader->setZfPath($path, '1.8');
  201. ]]></programlisting>
  202. <para>
  203. In this case, it will find the directory
  204. <filename>ZendFramework/1.8.4PL1/library/</filename>.
  205. </para>
  206. <para>
  207. In the final case, for <emphasis>production</emphasis>, we'll pin to a specific version
  208. -- 1.7.7, since that was what was available when Quality Assurance tested prior to our
  209. release.
  210. </para>
  211. <programlisting language="php"><![CDATA[
  212. $autoloader->setZfPath($path, '1.7.7');
  213. ]]></programlisting>
  214. <para>
  215. Predictably, it finds the directory <filename>ZendFramework/1.7.7/library/</filename>.
  216. </para>
  217. <para>
  218. You can also specify these values in the configuration file you use with
  219. <filename>Zend_Application</filename>. To do so, you'd specify the following
  220. information:
  221. </para>
  222. <programlisting language="ini"><![CDATA[
  223. [production]
  224. autoloaderZfPath = "path/to/ZendFramework"
  225. autoloaderZfVersion = "1.7.7"
  226. [qa]
  227. autoloaderZfVersion = "1.8"
  228. [development]
  229. autoloaderZfVersion = "latest"
  230. ]]></programlisting>
  231. <para>
  232. Note the different environment sections, and the different version specified in each
  233. environment; these factors will allow <classname>Zend_Application</classname> to
  234. configure the autoloader appropriately.
  235. </para>
  236. <warning>
  237. <title>Performance implications</title>
  238. <para>
  239. For best performance, either do not use this feature, or specify a specific Zend
  240. Framework version (i.e., not "latest", a major revision such as "1", or a minor
  241. revision such as "1.8"). Otherwise, the autoloader will need to scan the provided
  242. path for directories matching the criteria -- a somewhat expensive operation to
  243. perform on each request.
  244. </para>
  245. </warning>
  246. </sect2>
  247. <sect2 id="zend.loader.autoloader.interface">
  248. <title>The Autoloader Interface</title>
  249. <para>
  250. Besides being able to specify arbitrary callbacks as autoloaders,
  251. Zend Framework also defines an interface autoloading classes may
  252. imlement, <classname>Zend_Loader_Autoloader_Interface</classname>:
  253. </para>
  254. <programlisting language="php"><![CDATA[
  255. interface Zend_Loader_Autoloader_Interface
  256. {
  257. public function autoload($class);
  258. }
  259. ]]></programlisting>
  260. <para>
  261. When using this interface, you can simply pass a class instance to
  262. <classname>Zend_Loader_Autoloader</classname>'s <methodname>pushAutoloader()</methodname>
  263. and <methodname>unshiftAutoloader()</methodname> methods:
  264. </para>
  265. <programlisting language="php"><![CDATA[
  266. // Assume Foo_Autoloader implements Zend_Loader_Autoloader_Interface:
  267. $foo = new Foo_Autoloader();
  268. $autoloader->pushAutoloader($foo, 'Foo_');
  269. ]]></programlisting>
  270. </sect2>
  271. <sect2 id="zend.loader.autoloader.reference">
  272. <title>Autoloader Reference</title>
  273. <para>
  274. Below, please find a guide to the methods available in
  275. <classname>Zend_Loader_Autoloader</classname>.
  276. </para>
  277. <table id="zend.loader.autoloader.reference.api">
  278. <title>Zend_Loader_Autoloader Methods</title>
  279. <tgroup cols="4">
  280. <thead>
  281. <row>
  282. <entry>Method</entry>
  283. <entry>Return Value</entry>
  284. <entry>Parameters</entry>
  285. <entry>Description</entry>
  286. </row>
  287. </thead>
  288. <tbody>
  289. <row>
  290. <entry><methodname>getInstance()</methodname></entry>
  291. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  292. <entry>N/A</entry>
  293. <entry><para>
  294. Retrieve the <classname>Zend_Loader_Autoloader</classname>
  295. singleton instance. On first retrieval, it registers
  296. itself with <code>spl_autoload</code>. This method
  297. is static.
  298. </para></entry>
  299. </row>
  300. <row>
  301. <entry><methodname>resetInstance()</methodname></entry>
  302. <entry><code>void</code></entry>
  303. <entry>N/A</entry>
  304. <entry><para>
  305. Resets the state of the <classname>Zend_Loader_Autoloader</classname>
  306. singleton instance to it's original state,
  307. unregistering all autoloader callbacks and all
  308. registered namespaces.
  309. </para></entry>
  310. </row>
  311. <row>
  312. <entry><methodname>autoload($class)</methodname></entry>
  313. <entry><code>string|<constant>FALSE</constant></code></entry>
  314. <entry><itemizedlist>
  315. <listitem><para>
  316. <varname>$class</varname>, <emphasis>required</emphasis>.
  317. A string class name to load.
  318. </para></listitem>
  319. </itemizedlist></entry>
  320. <entry><para>
  321. Attempt to resolve a class name to a file and load
  322. it.
  323. </para></entry>
  324. </row>
  325. <row>
  326. <entry><methodname>setDefaultAutoloader($callback)</methodname></entry>
  327. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  328. <entry><itemizedlist>
  329. <listitem><para>
  330. <varname>$callback</varname>, <emphasis>required</emphasis>.
  331. </para></listitem>
  332. </itemizedlist></entry>
  333. <entry><para>
  334. Specify an alternate <acronym>PHP</acronym> callback to use for the
  335. default autoloader implementation.
  336. </para></entry>
  337. </row>
  338. <row>
  339. <entry><methodname>getDefaultAutoloader()</methodname></entry>
  340. <entry><code>callback</code></entry>
  341. <entry>N/A</entry>
  342. <entry><para>
  343. Retrieve the default autoloader implementation; by
  344. default, this is
  345. <methodname>Zend_Loader::loadClass()</methodname>.
  346. </para></entry>
  347. </row>
  348. <row>
  349. <entry><methodname>setAutoloaders(array $autoloaders)</methodname></entry>
  350. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  351. <entry><itemizedlist>
  352. <listitem><para>
  353. <varname>$autoloaders</varname>, <emphasis>required</emphasis>.
  354. </para></listitem>
  355. </itemizedlist></entry>
  356. <entry><para>
  357. Set a list of concrete autoloaders to use in the
  358. autoloader stack. Each item in the autoloaders array
  359. must be a <acronym>PHP</acronym> callback.
  360. </para></entry>
  361. </row>
  362. <row>
  363. <entry><methodname>getAutoloaders()</methodname></entry>
  364. <entry><type>Array</type></entry>
  365. <entry>N/A</entry>
  366. <entry><para>
  367. Retrieve the internal autoloader stack.
  368. </para></entry>
  369. </row>
  370. <row>
  371. <entry><methodname>getNamespaceAutoloaders($namespace)</methodname></entry>
  372. <entry><type>Array</type></entry>
  373. <entry><itemizedlist>
  374. <listitem><para>
  375. <varname>$namespace</varname>, <emphasis>required</emphasis>
  376. </para></listitem>
  377. </itemizedlist></entry>
  378. <entry><para>
  379. Fetch all autoloaders that have registered to load a
  380. specific namespace.
  381. </para></entry>
  382. </row>
  383. <row>
  384. <entry><methodname>registerNamespace($namespace)</methodname></entry>
  385. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  386. <entry><itemizedlist>
  387. <listitem><para>
  388. <varname>$namespace</varname>, <emphasis>required</emphasis>.
  389. </para></listitem>
  390. </itemizedlist></entry>
  391. <entry><para>
  392. Register one or more namespaces with the default
  393. autoloader. If <varname>$namespace</varname> is a string,
  394. it registers that namespace; if it's an array of
  395. strings, registers each as a namespace.
  396. </para></entry>
  397. </row>
  398. <row>
  399. <entry><methodname>unregisterNamespace($namespace)</methodname></entry>
  400. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  401. <entry><itemizedlist>
  402. <listitem><para>
  403. <varname>$namespace</varname>, <emphasis>required</emphasis>.
  404. </para></listitem>
  405. </itemizedlist></entry>
  406. <entry><para>
  407. Unregister one or more namespaces from the default
  408. autoloader. If <varname>$namespace</varname> is a string,
  409. it unregisters that namespace; if it's an array of
  410. strings, unregisters each as a namespace.
  411. </para></entry>
  412. </row>
  413. <row>
  414. <entry><methodname>getRegisteredNamespace()</methodname></entry>
  415. <entry><type>Array</type></entry>
  416. <entry>N/A</entry>
  417. <entry><para>
  418. Returns an array of all namespaces registered with
  419. the default autoloader.
  420. </para></entry>
  421. </row>
  422. <row>
  423. <entry><methodname>suppressNotFoundWarnings($flag = null)</methodname></entry>
  424. <entry><code>boolean|Zend_Loader_Autoloader</code></entry>
  425. <entry><itemizedlist>
  426. <listitem><para>
  427. <varname>$flag</varname>, <emphasis>optional</emphasis>.
  428. </para></listitem>
  429. </itemizedlist></entry>
  430. <entry><para>
  431. Set or retrieve the value of the flag used to
  432. indicate whether the default autoloader
  433. implementation should suppress "file not found"
  434. warnings. If no arguments or a <constant>NULL</constant> value is
  435. passed, returns a boolean indicating the status of the flag;
  436. if a boolean is passed, the flag is set to that
  437. value and the autoloader instance is returned (to
  438. allow method chaining).
  439. </para></entry>
  440. </row>
  441. <row>
  442. <entry><methodname>setFallbackAutoloader($flag)</methodname></entry>
  443. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  444. <entry><itemizedlist>
  445. <listitem><para>
  446. <varname>$flag</varname>, <emphasis>required</emphasis>.
  447. </para></listitem>
  448. </itemizedlist></entry>
  449. <entry><para>
  450. Set the value of the flag used to indicate whether
  451. or not the default autoloader should be used as a
  452. fallback or catch-all autoloader for all namespaces.
  453. </para></entry>
  454. </row>
  455. <row>
  456. <entry><methodname>isFallbackAutoloader()</methodname></entry>
  457. <entry><type>Boolean</type></entry>
  458. <entry>N/A</entry>
  459. <entry><para>
  460. Retrieve the value of the flag used to indicate whether
  461. or not the default autoloader should be used as a
  462. fallback or catch-all autoloader for all namespaces.
  463. By default, this is <constant>FALSE</constant>.
  464. </para></entry>
  465. </row>
  466. <row>
  467. <entry><methodname>getClassAutoloaders($class)</methodname></entry>
  468. <entry><type>Array</type></entry>
  469. <entry><itemizedlist>
  470. <listitem><para>
  471. <varname>$class</varname>, <emphasis>required</emphasis>.
  472. </para></listitem>
  473. </itemizedlist></entry>
  474. <entry><para>
  475. Get the list of namespaced autoloaders that could
  476. potentially match the provided class. If none match,
  477. all global (non-namespaced) autoloaders are
  478. returned.
  479. </para></entry>
  480. </row>
  481. <row>
  482. <entry><methodname>unshiftAutoloader($callback, $namespace = '')</methodname></entry>
  483. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  484. <entry><itemizedlist>
  485. <listitem><para>
  486. <varname>$callback</varname>, <emphasis>required</emphasis>.
  487. A valid <acronym>PHP</acronym> callback
  488. </para></listitem>
  489. <listitem><para>
  490. <varname>$namespace</varname>, <emphasis>optional</emphasis>.
  491. A string representing a class prefix namespace.
  492. </para></listitem>
  493. </itemizedlist></entry>
  494. <entry><para>
  495. Add a concrete autoloader implementation to the
  496. beginning of the internal autoloader stack. If a
  497. namespace is provided, that namespace will be used
  498. to match optimistically; otherwise, the autoloader
  499. will be considered a global autoloader.
  500. </para></entry>
  501. </row>
  502. <row>
  503. <entry><methodname>pushAutoloader($callback, $namespace = '')</methodname></entry>
  504. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  505. <entry><itemizedlist>
  506. <listitem><para>
  507. <varname>$callback</varname>, <emphasis>required</emphasis>.
  508. A valid <acronym>PHP</acronym> callback
  509. </para></listitem>
  510. <listitem><para>
  511. <varname>$namespace</varname>, <emphasis>optional</emphasis>.
  512. A string representing a class prefix namespace.
  513. </para></listitem>
  514. </itemizedlist></entry>
  515. <entry><para>
  516. Add a concrete autoloader implementation to the
  517. end of the internal autoloader stack. If a
  518. namespace is provided, that namespace will be used
  519. to match optimistically; otherwise, the autoloader
  520. will be considered a global autoloader.
  521. </para></entry>
  522. </row>
  523. <row>
  524. <entry><methodname>removeAutoloader($callback, $namespace = '')</methodname></entry>
  525. <entry><classname>Zend_Loader_Autoloader</classname></entry>
  526. <entry><itemizedlist>
  527. <listitem><para>
  528. <varname>$callback</varname>, <emphasis>required</emphasis>.
  529. A valid <acronym>PHP</acronym> callback
  530. </para></listitem>
  531. <listitem><para>
  532. <varname>$namespace</varname>, <emphasis>optional</emphasis>.
  533. A string representing a class prefix namespace,
  534. or an array of namespace strings.
  535. </para></listitem>
  536. </itemizedlist></entry>
  537. <entry><para>
  538. Remove a concrete autoloader implementation from
  539. the internal autoloader stack. If a namespace or
  540. namespaces are provided, the callback will be
  541. removed from that namespace or namespaces only.
  542. </para></entry>
  543. </row>
  544. </tbody>
  545. </tgroup>
  546. </table>
  547. </sect2>
  548. </sect1>