Zend_Loader-StandardAutoloader.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.loader.standard-autoloader">
  4. <title>The StandardAutoloader</title>
  5. <sect2 id="zend.loader.standard-autoloader.intro">
  6. <title>Overview</title>
  7. <para>
  8. <classname>Zend_Loader_StandardAutoloader</classname> is designed as a <ulink
  9. url="http://groups.google.com/group/php-standards/web/psr-0-final-proposal">PSR-0</ulink>-compliant
  10. autoloader. It assumes a 1:1 mapping of the namespace+classname to the filesystem,
  11. wherein namespace separators and underscores are translated to directory separators. A
  12. simple statement that illustrates how resolution works is as follows:
  13. </para>
  14. <programlisting language="php"><![CDATA[
  15. $filename = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $classname)
  16. . '.php';
  17. ]]></programlisting>
  18. <para>
  19. Previous incarnations of PSR-0-compliant autoloaders in Zend Framework have relied upon
  20. the <varname>include_path</varname> for file lookups. This has led to a number of
  21. issues:
  22. </para>
  23. <itemizedlist>
  24. <listitem>
  25. <para>
  26. Due to the use of <function>include</function>, if the file is not
  27. found, a warning is raised -- even if another autoloader is capable of resolving
  28. the class later.
  29. </para>
  30. </listitem>
  31. <listitem>
  32. <para>
  33. Documenting how to setup the <varname>include_path</varname> has proven to be
  34. a difficult concept to convey.
  35. </para>
  36. </listitem>
  37. <listitem>
  38. <para>
  39. If multiple Zend Framework installations exist on the
  40. <varname>include_path</varname>, the first one on the path wins -- even if that
  41. was not the one the developer intended.
  42. </para>
  43. </listitem>
  44. </itemizedlist>
  45. <para>
  46. To solve these problems, the <classname>StandardAutoloader</classname> by default
  47. requires that you explicitly register namespace/path pairs (or vendor prefix/path
  48. pairs), and will only load a file if it exists within the given path. Multiple pairs may
  49. be provided.
  50. </para>
  51. <para>
  52. As a measure of last resort, you may also use the
  53. <classname>StandardAutoloader</classname> as a "fallback" autoloader -- one that will
  54. look for classes of any namespace or vendor prefix on the
  55. <classname>include_path</classname>. This practice is not recommended, however, due to
  56. performance implications.
  57. </para>
  58. <para>
  59. Finally, as with all autoloaders in Zend Framework, the
  60. <classname>StandardAutoloader</classname> is capable of registering itself with PHP's
  61. SPL autoloader registry.
  62. </para>
  63. <note>
  64. <title>Vocabulary: Namespaces vs. Vendor Prefixes</title>
  65. <para>
  66. In terms of autloading, a "namespace" corresponds to PHP's own definition of
  67. namespaces in PHP versions 5.3 and above.
  68. </para>
  69. <para>
  70. A "vendor prefix" refers to the practice, popularized in PHP versions prior to 5.3,
  71. of providing a pseudo-namespace in the form of underscore-separated words in class
  72. names. As an example, the class <classname>Phly_Couch_Document</classname> uses a
  73. vendor prefix of "Phly", and a component prefix of "Phly_Couch" -- but it is a class
  74. sitting in the global namespace within PHP 5.3.
  75. </para>
  76. <para>
  77. The <classname>StandardAutoloader</classname> is capable of loading either
  78. namespaced or vendor prefixed class names, but treats them separately when
  79. attempting to match them to an appropriate path.
  80. </para>
  81. </note>
  82. </sect2>
  83. <sect2 id="zend.loader.standard-autoloader.quick-start">
  84. <title>Quick Start</title>
  85. <para>
  86. Basic use of the <classname>StandardAutoloader</classname> requires simply registering
  87. namespace/path pairs. This can either be done at instantiation, or via explicit method
  88. calls after the object has been initialized. Calling <methodname>register()</methodname>
  89. will register the autoloader with the SPL autoloader registry.
  90. </para>
  91. <para>
  92. By default, the class will register the "Zend" namespace to the directory above where
  93. its own classfile is located on the filesystem.
  94. </para>
  95. <example id="zend.loader.standard-autoloader.quick-start.example-manual-configuration">
  96. <title>Manual Configuration</title>
  97. <programlisting language="php"><![CDATA[
  98. // This example assumes ZF is on your include_path.
  99. // You could also load the autoloader class from a path relative to the
  100. // current script, or via an absolute path.
  101. require_once 'Zend/Loader/StandardAutoloader.php';
  102. $loader = new Zend_Loader_StandardAutoloader();
  103. // Register the "Phly" namespace:
  104. $loader->registerNamespace('Phly', APPLICATION_PATH . '/../library/Phly');
  105. // Register the "Scapi" vendor prefix:
  106. $loader->registerPrefix('Scapi', APPLICATION_PATH . '/../library/Scapi');
  107. // Optionally, specify the autoloader as a "fallback" autoloader;
  108. // this is not recommended.
  109. $loader->setFallbackAutoloader(true);
  110. // Register with spl_autoload:
  111. $loader->register();
  112. ]]></programlisting>
  113. </example>
  114. <example id="zend.loader.standard-autoloader.quick-start.example-constructor-configuration">
  115. <title>Configuration at Instantiation</title>
  116. <para>
  117. The <classname>StandardAutoloader</classname> may also be configured at
  118. instantiation. Please note:
  119. </para>
  120. <itemizedlist>
  121. <listitem>
  122. <para>
  123. The argument passed may be either an array or a
  124. <interface>Traversable</interface> object (such as a
  125. <classname>Zend_Config</classname> object.
  126. </para>
  127. </listitem>
  128. <listitem>
  129. <para>
  130. The argument passed is also a valid argument for passing to the
  131. <methodname>setOptions()</methodname> method.
  132. </para>
  133. </listitem>
  134. </itemizedlist>
  135. <para>
  136. The following is equivalent to the previous example.
  137. </para>
  138. <programlisting language="php"><![CDATA[
  139. require_once 'Zend/Loader/StandardAutoloader.php';
  140. $loader = new Zend_Loader_StandardAutoloader(array(
  141. 'namespaces' => array(
  142. 'Phly' => APPLICATION_PATH . '/../library/Phly',
  143. ),
  144. 'prefixes' => array(
  145. 'Scapi' => APPLICATION_PATH . '/../library/Scapi',
  146. ),
  147. 'fallback_autoloader' => true,
  148. ));
  149. // Register with spl_autoload:
  150. $loader->register();
  151. ]]></programlisting>
  152. </example>
  153. </sect2>
  154. <sect2 id="zend.loader.standard-autoloader.options">
  155. <title>Configuration Options</title>
  156. <para>
  157. The <classname>StandardAutoloader</classname> defines the following options.
  158. </para>
  159. <variablelist>
  160. <title>StandardAutoloader Options</title>
  161. <varlistentry>
  162. <term>namespaces</term>
  163. <listitem>
  164. <para>
  165. An associative array of namespace/path pairs. The path should be an absolute
  166. path or path relative to the calling script, and contain only classes that
  167. live in that namespace (or its subnamespaces). By default, the "Zend"
  168. namespace is registered, pointing to the arent directory of the file
  169. defining the <classname>StandardAutoloader</classname>.
  170. </para>
  171. </listitem>
  172. </varlistentry>
  173. <varlistentry>
  174. <term>prefixes</term>
  175. <listitem>
  176. <para>
  177. An associative array of vendor prefix/path pairs. The path should be an absolute
  178. path or path relative to the calling script, and contain only classes that
  179. begin with the provided vendor prefix.
  180. </para>
  181. </listitem>
  182. </varlistentry>
  183. <varlistentry>
  184. <term>fallback_autoloader</term>
  185. <listitem>
  186. <para>
  187. A boolean value indicating whether or not this instance should act as a
  188. "fallback" autoloader (i.e., look for classes of any namespace or vendor
  189. prefix on the <varname>include_path</varname>). By default,
  190. <constant>false</constant>.
  191. </para>
  192. </listitem>
  193. </varlistentry>
  194. </variablelist>
  195. </sect2>
  196. <sect2 id="zend.loader.standard-autoloader.methods">
  197. <title>Available Methods</title>
  198. <refentry id="zend.loader.standard-autoloader.methods.constructor">
  199. <refnamediv>
  200. <refname>__construct</refname>
  201. <refpurpose>Initialize a new instance of the object</refpurpose>
  202. </refnamediv>
  203. <refsynopsisdiv>
  204. <methodsynopsis>
  205. <methodname>__construct</methodname>
  206. <methodparam>
  207. <funcparams>$options = null</funcparams>
  208. </methodparam>
  209. </methodsynopsis>
  210. </refsynopsisdiv>
  211. <refsection>
  212. <title>Constructor</title>
  213. <para>
  214. Takes an optional <varname>$options</varname> argument. This argument may be an
  215. associative array or <interfacename>Traversable</interfacename> object. If not
  216. null, the argument is passed to <link linkend="zend.loader.standard-autoloader.methods.set-options"><methodname>setOptions()</methodname></link>.
  217. </para>
  218. </refsection>
  219. </refentry>
  220. <refentry id="zend.loader.standard-autoloader.methods.set-options">
  221. <refnamediv>
  222. <refname>setOptions</refname>
  223. <refpurpose>Set object state based on provided options.</refpurpose>
  224. </refnamediv>
  225. <refsynopsisdiv>
  226. <methodsynopsis>
  227. <methodname>setOptions</methodname>
  228. <methodparam>
  229. <funcparams>$options</funcparams>
  230. </methodparam>
  231. </methodsynopsis>
  232. </refsynopsisdiv>
  233. <refsection>
  234. <title>setOptions()</title>
  235. <para>
  236. Takes an argument of either an associative array or
  237. <interfacename>Traversable</interfacename> object. Recognized keys are detailed
  238. under <xref linkend="zend.loader.standard-autoloader.options"/>, with the
  239. following behaviors:
  240. </para>
  241. <itemizedlist>
  242. <listitem>
  243. <para>
  244. The <varname>namespaces</varname> value will be passed to <link linkend="zend.loader.standard-autoloader.methods.register-namespaces">registerNamespaces()</link>.
  245. </para>
  246. </listitem>
  247. <listitem>
  248. <para>
  249. The <varname>prefixes</varname> value will be passed to <link linkend="zend.loader.standard-autoloader.methods.register-prefixes">registerPrefixes()</link>.
  250. </para>
  251. </listitem>
  252. <listitem>
  253. <para>
  254. The <varname>fallback_autoloader</varname> value will be passed to <link linkend="zend.loader.standard-autoloader.methods.set-fallback-autoloader">setFallbackAutoloader()</link>.
  255. </para>
  256. </listitem>
  257. </itemizedlist>
  258. </refsection>
  259. </refentry>
  260. <refentry id="zend.loader.standard-autoloader.methods.set-fallback-autoloader">
  261. <refnamediv>
  262. <refname>setFallbackAutoloader</refname>
  263. <refpurpose>Enable/disable fallback autoloader status</refpurpose>
  264. </refnamediv>
  265. <refsynopsisdiv>
  266. <methodsynopsis>
  267. <methodname>setFallbackAutoloader</methodname>
  268. <methodparam>
  269. <funcparams>$flag</funcparams>
  270. </methodparam>
  271. </methodsynopsis>
  272. </refsynopsisdiv>
  273. <refsection>
  274. <title>setFallbackAutoloader()</title>
  275. <para>
  276. Takes a boolean flag indicating whether or not to act as a fallback autoloader
  277. when registered with the SPL autoloader.
  278. </para>
  279. </refsection>
  280. </refentry>
  281. <refentry id="zend.loader.standard-autoloader.methods.is-fallback-autoloader">
  282. <refnamediv>
  283. <refname>isFallbackAutoloader</refname>
  284. <refpurpose>Query fallback autoloader status</refpurpose>
  285. </refnamediv>
  286. <refsynopsisdiv>
  287. <methodsynopsis>
  288. <methodname>isFallbackAutoloader</methodname>
  289. </methodsynopsis>
  290. </refsynopsisdiv>
  291. <refsection>
  292. <title>isFallbackAutoloader()</title>
  293. <para>
  294. Indicates whether or not this instance is flagged as a fallback autoloader.
  295. </para>
  296. </refsection>
  297. </refentry>
  298. <refentry id="zend.loader.standard-autoloader.methods.register-namespace">
  299. <refnamediv>
  300. <refname>registerNamespace</refname>
  301. <refpurpose>Register a namespace with the autoloader</refpurpose>
  302. </refnamediv>
  303. <refsynopsisdiv>
  304. <methodsynopsis>
  305. <methodname>registerNamespace</methodname>
  306. <methodparam>
  307. <funcparams>$namespace, $directory</funcparams>
  308. </methodparam>
  309. </methodsynopsis>
  310. </refsynopsisdiv>
  311. <refsection>
  312. <title>registerNamespace()</title>
  313. <para>
  314. Register a namespace with the autoloader, pointing it to a specific directory on
  315. the filesystem for class resolution. For classes matching that initial
  316. namespace, the autoloader will then perform lookups within that directory.
  317. </para>
  318. </refsection>
  319. </refentry>
  320. <refentry id="zend.loader.standard-autoloader.methods.register-namespaces">
  321. <refnamediv>
  322. <refname>registerNamespaces</refname>
  323. <refpurpose>Register multiple namespaces with the autoloader</refpurpose>
  324. </refnamediv>
  325. <refsynopsisdiv>
  326. <methodsynopsis>
  327. <methodname>registerNamespaces</methodname>
  328. <methodparam>
  329. <funcparams>$namespaces</funcparams>
  330. </methodparam>
  331. </methodsynopsis>
  332. </refsynopsisdiv>
  333. <refsection>
  334. <title>registerNamespaces()</title>
  335. <para>
  336. Accepts either an array or <interfacename>Traversable</interfacename> object. It
  337. will then iterate through the argument, and pass each item to <link linkend="zend.loader.standard-autoloader.methods.register-namespace">registerNamespace()</link>.
  338. </para>
  339. </refsection>
  340. </refentry>
  341. <refentry id="zend.loader.standard-autoloader.methods.register-prefix">
  342. <refnamediv>
  343. <refname>registerPrefix</refname>
  344. <refpurpose>Register a vendor prefix with the autoloader.</refpurpose>
  345. </refnamediv>
  346. <refsynopsisdiv>
  347. <methodsynopsis>
  348. <methodname>registerPrefix</methodname>
  349. <methodparam>
  350. <funcparams>$prefix, $directory</funcparams>
  351. </methodparam>
  352. </methodsynopsis>
  353. </refsynopsisdiv>
  354. <refsection>
  355. <title>registerPrefix()</title>
  356. <para>
  357. Register a vendor prefix with the autoloader, pointing it to a specific
  358. directory on the filesystem for class resolution. For classes matching that
  359. initial vendor prefix, the autoloader will then perform lookups within that
  360. directory.
  361. </para>
  362. </refsection>
  363. </refentry>
  364. <refentry id="zend.loader.standard-autoloader.methods.register-prefixes">
  365. <refnamediv>
  366. <refname>registerPrefixes</refname>
  367. <refpurpose>Register many vendor prefixes with the autoloader</refpurpose>
  368. </refnamediv>
  369. <refsynopsisdiv>
  370. <methodsynopsis>
  371. <methodname>registerPrefixes</methodname>
  372. <methodparam>
  373. <funcparams>$prefixes</funcparams>
  374. </methodparam>
  375. </methodsynopsis>
  376. </refsynopsisdiv>
  377. <refsection>
  378. <title>registerPrefixes()</title>
  379. <para>
  380. Accepts either an array or <interfacename>Traversable</interfacename> object. It
  381. will then iterate through the argument, and pass each item to <link linkend="zend.loader.standard-autoloader.methods.register-prefix">registerPrefix()</link>.
  382. </para>
  383. </refsection>
  384. </refentry>
  385. <refentry id="zend.loader.standard-autoloader.methods.autoload">
  386. <refnamediv>
  387. <refname>autoload</refname>
  388. <refpurpose>Attempt to load a class.</refpurpose>
  389. </refnamediv>
  390. <refsynopsisdiv>
  391. <methodsynopsis>
  392. <methodname>autoload</methodname>
  393. <methodparam>
  394. <funcparams>$class</funcparams>
  395. </methodparam>
  396. </methodsynopsis>
  397. </refsynopsisdiv>
  398. <refsection>
  399. <title>autoload()</title>
  400. <para>
  401. Attempts to load the class specified. Returns a boolean
  402. <constant>false</constant> on failure, or a string indicating the class loaded
  403. on success.
  404. </para>
  405. </refsection>
  406. </refentry>
  407. <refentry id="zend.loader.standard-autoloader.methods.register">
  408. <refnamediv>
  409. <refname>register</refname>
  410. <refpurpose>Register with spl_autoload.</refpurpose>
  411. </refnamediv>
  412. <refsynopsisdiv>
  413. <methodsynopsis>
  414. <methodname>register</methodname>
  415. </methodsynopsis>
  416. </refsynopsisdiv>
  417. <refsection>
  418. <title>register()</title>
  419. <para>
  420. Registers the <methodname>autoload()</methodname> method of the current instance
  421. with <function>spl_autoload_register()</function>.
  422. </para>
  423. </refsection>
  424. </refentry>
  425. </sect2>
  426. <sect2 id="zend.loader.standard-autoloader.examples">
  427. <title>Examples</title>
  428. <para>
  429. Please review the <link linkend="zend.loader.standard-autoloader.quick-start">examples
  430. in the quick start</link> for usage.
  431. </para>
  432. </sect2>
  433. </sect1>