Zend_Loader.xml 12 KB

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