Zend_Loader.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.loader.load">
  4. <title>Loading Files and Classes Dynamically</title>
  5. <para>
  6. The <classname>Zend_Loader</classname> class includes methods to help you load files
  7. dynamically.
  8. </para>
  9. <tip>
  10. <title>Zend_Loader vs. require_once()</title>
  11. <para>
  12. The <classname>Zend_Loader</classname> methods are best used if the filename you need to
  13. load is variable. For example, if it is based on a parameter from
  14. user input or method argument. If you are loading a file or a
  15. class whose name is constant, there is no benefit to using
  16. <classname>Zend_Loader</classname> over using traditional <acronym>PHP</acronym>
  17. functions such as <ulink
  18. url="http://php.net/require_once"><methodname>require_once()</methodname></ulink>.
  19. </para>
  20. </tip>
  21. <sect2 id="zend.loader.load.file">
  22. <title>Loading Files</title>
  23. <para>
  24. The static method <methodname>Zend_Loader::loadFile()</methodname> loads a
  25. <acronym>PHP</acronym> file. The file loaded may contain any <acronym>PHP</acronym>
  26. code. The method is a wrapper for the <acronym>PHP</acronym> function
  27. <ulink url="http://php.net/include"><methodname>include()</methodname></ulink>.
  28. This method returns boolean <constant>FALSE</constant> on failure, for example
  29. if the specified file does not exist.
  30. </para>
  31. <example id="zend.loader.load.file.example">
  32. <title>Example of the loadFile() Method</title>
  33. <programlisting language="php"><![CDATA[
  34. Zend_Loader::loadFile($filename, $dirs=null, $once=false);
  35. ]]></programlisting>
  36. </example>
  37. <para>
  38. The <varname>$filename</varname> argument specifies the filename to load,
  39. which must not contain any path information.
  40. A security check is performed on <varname>$filename</varname>.
  41. The <varname>$filename</varname> may only contain alphanumeric characters,
  42. dashes ("-"), underscores ("_"), or periods (".").
  43. No such restriction is placed on the <varname>$dirs</varname> argument.
  44. </para>
  45. <para>
  46. The <varname>$dirs</varname> argument specifies which directories to search for the
  47. file in. If the value is <constant>NULL</constant>, only the <code>include_path</code>
  48. is searched; if the value is a string or an array, the directory or directories
  49. specified will be searched, followed by the <code>include_path</code>.
  50. </para>
  51. <para>
  52. The <varname>$once</varname> argument is a boolean. If <constant>TRUE</constant>,
  53. <methodname>Zend_Loader::loadFile()</methodname> uses the <acronym>PHP</acronym>
  54. function <ulink
  55. url="http://php.net/include"><methodname>include_once()</methodname></ulink>
  56. for loading the file, otherwise the <acronym>PHP</acronym> function
  57. <ulink url="http://php.net/include_once"><methodname>include()</methodname></ulink>
  58. is used.
  59. </para>
  60. </sect2>
  61. <sect2 id="zend.loader.load.class">
  62. <title>Loading Classes</title>
  63. <para>
  64. The static method <methodname>Zend_Loader::loadClass($class, $dirs)</methodname>
  65. loads a <acronym>PHP</acronym> file and then checks for the existence of the class.
  66. </para>
  67. <example id="zend.loader.load.class.example">
  68. <title>Example of the loadClass() Method</title>
  69. <programlisting language="php"><![CDATA[
  70. Zend_Loader::loadClass('Container_Tree',
  71. array(
  72. '/home/production/mylib',
  73. '/home/production/myapp'
  74. )
  75. );
  76. ]]></programlisting>
  77. </example>
  78. <para>
  79. The string specifying the class is converted to a relative path
  80. by substituting underscores with directory separators for your OS, and appending
  81. '.php'. In the example above, 'Container_Tree' becomes 'Container\\Tree.php' on Windows.
  82. </para>
  83. <para>
  84. If <varname>$dirs</varname> is a string or an array,
  85. <methodname>Zend_Loader::loadClass()</methodname> searches the directories in
  86. the order supplied. The first matching file is loaded. If the file
  87. does not exist in the specified <varname>$dirs</varname>, then the
  88. <code>include_path</code> for the <acronym>PHP</acronym> environment is searched.
  89. </para>
  90. <para>
  91. If the file is not found or the class does not exist after the load,
  92. <methodname>Zend_Loader::loadClass()</methodname> throws a
  93. <classname>Zend_Exception</classname>.
  94. </para>
  95. <para>
  96. <methodname>Zend_Loader::loadFile()</methodname> is used for loading, so the
  97. class name may only contain alphanumeric characters and the hyphen
  98. ('-'), underscore ('_'), and period ('.').
  99. </para>
  100. <note>
  101. <title>Loading Classes from PHP Namespaces</title>
  102. <para>
  103. Starting in version 1.10.0, Zend Framework now allows loading classes from
  104. <acronym>PHP</acronym> namespaces. This support follows the same guidelines and
  105. implementation as that found in the <ulink
  106. url="http://groups.google.com/group/php-standards/web/psr-0-final-proposal">PHP
  107. Framework Interop Group PSR-0</ulink> reference implementation.
  108. </para>
  109. <para>
  110. Under this guideline, the following rules apply:
  111. </para>
  112. <itemizedlist>
  113. <listitem>
  114. <para>
  115. Each namespace separator is converted to a
  116. <constant>DIRECTORY_SEPARATOR</constant> when loading from the file system.
  117. </para>
  118. </listitem>
  119. <listitem>
  120. <para>
  121. Each "_" character in the <emphasis>CLASS NAME</emphasis> is converted to a
  122. <constant>DIRECTORY_SEPARATOR</constant>. The "_" character has no special
  123. meaning in the namespace.
  124. </para>
  125. </listitem>
  126. <listitem>
  127. <para>
  128. The fully-qualified namespace and class is suffixed with ".php" when loading
  129. from the file system.
  130. </para>
  131. </listitem>
  132. </itemizedlist>
  133. <para>
  134. As examples:
  135. </para>
  136. <itemizedlist>
  137. <listitem>
  138. <para>
  139. <classname>\Doctrine\Common\IsolatedClassLoader</classname> =&gt;
  140. <filename>/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php</filename>
  141. </para>
  142. </listitem>
  143. <listitem>
  144. <para>
  145. <classname>\namespace\package\Class_Name</classname> =&gt;
  146. <filename>/path/to/project/lib/vendor/namespace/package/Class/Name.php</filename>
  147. </para>
  148. </listitem>
  149. <listitem>
  150. <para>
  151. <classname>\namespace\package_name\Class_Name</classname> =&gt;
  152. <filename>/path/to/project/lib/vendor/namespace/package_name/Class/Name.php</filename>
  153. </para>
  154. </listitem>
  155. </itemizedlist>
  156. </note>
  157. </sect2>
  158. <sect2 id="zend.loader.load.isreadable">
  159. <title>Testing if a File is Readable</title>
  160. <para>
  161. The static method <methodname>Zend_Loader::isReadable($pathname)</methodname>
  162. returns <constant>TRUE</constant> if a file at the specified pathname exists
  163. and is readable, <constant>FALSE</constant> otherwise.
  164. </para>
  165. <example id="zend.loader.load.isreadable.example">
  166. <title>Example of isReadable() method</title>
  167. <programlisting language="php"><![CDATA[
  168. if (Zend_Loader::isReadable($filename)) {
  169. // do something with $filename
  170. }
  171. ]]></programlisting>
  172. </example>
  173. <para>
  174. The <varname>$filename</varname> argument specifies the filename to
  175. check. This may contain path information.
  176. This method is a wrapper for the <acronym>PHP</acronym> function
  177. <ulink url="http://php.net/is_readable"><methodname>is_readable()</methodname></ulink>.
  178. The <acronym>PHP</acronym> function does not search the <code>include_path</code>,
  179. while <methodname>Zend_Loader::isReadable()</methodname> does.
  180. </para>
  181. </sect2>
  182. <sect2 id="zend.loader.load.autoload">
  183. <title>Using the Autoloader</title>
  184. <para>
  185. The <classname>Zend_Loader</classname> class contains a method you can register with the
  186. <acronym>PHP</acronym> SPL autoloader. <methodname>Zend_Loader::autoload()</methodname>
  187. is the callback method. As a convenience, <classname>Zend_Loader</classname> provides
  188. the <methodname>registerAutoload()</methodname> function to register its
  189. <methodname>autoload()</methodname> method. If the <code>spl_autoload</code>
  190. extension is not present in your <acronym>PHP</acronym> environment, then the
  191. <methodname>registerAutoload()</methodname> method throws a
  192. <classname>Zend_Exception</classname>.
  193. </para>
  194. <example id="zend.loader.load.autoload.example">
  195. <title>Example of registering the autoloader callback method</title>
  196. <programlisting language="php"><![CDATA[
  197. Zend_Loader::registerAutoload();
  198. ]]></programlisting>
  199. </example>
  200. <para>
  201. After registering the Zend Framework autoload callback, you can
  202. reference classes from Zend Framework without having to load
  203. them explicitly. The <methodname>autoload()</methodname> method uses
  204. <methodname>Zend_Loader::loadClass()</methodname> automatically when you
  205. reference a class.
  206. </para>
  207. <para>
  208. If you have extended the <classname>Zend_Loader</classname> class, you can give an
  209. optional argument to <methodname>registerAutoload()</methodname>, to specify
  210. the class from which to register an <methodname>autoload()</methodname> method.
  211. </para>
  212. <example id="zend.loader.load.autoload.example-extended">
  213. <title>Example of registering the autoload callback method from an
  214. extended class</title>
  215. <para>
  216. Because of the semantics of static function references in <acronym>PHP</acronym>,
  217. you must implement code for both <methodname>loadClass()</methodname>
  218. and <methodname>autoload()</methodname>, and the <methodname>autoload()</methodname>
  219. must call <methodname>self::loadClass()</methodname>. If your
  220. <methodname>autoload()</methodname> method delegates to its parent to
  221. call <methodname>self::loadClass()</methodname>, then it calls the
  222. method of that name in the parent class, not the subclass.
  223. </para>
  224. <programlisting language="php"><![CDATA[
  225. class My_Loader extends Zend_Loader
  226. {
  227. public static function loadClass($class, $dirs = null)
  228. {
  229. parent::loadClass($class, $dirs);
  230. }
  231. public static function autoload($class)
  232. {
  233. try {
  234. self::loadClass($class);
  235. return $class;
  236. } catch (Exception $e) {
  237. return false;
  238. }
  239. }
  240. }
  241. Zend_Loader::registerAutoload('My_Loader');
  242. ]]></programlisting>
  243. </example>
  244. <para>
  245. You can remove an autoload callback. The
  246. <methodname>registerAutoload()</methodname> has an optional second argument,
  247. which is <constant>TRUE</constant> by default. If this argument is
  248. <constant>FALSE</constant>, the autoload callback is unregistered from the
  249. SPL autoload stack.
  250. </para>
  251. </sect2>
  252. </sect1>
  253. <!--
  254. vim:se ts=4 sw=4 et:
  255. -->