Zend_Loader.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. which emits a <acronym>PHP</acronym> warning 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 include_path
  48. is searched; if the value is a string or an array, the directory or directories
  49. specified will be searched, followed by the <property>include_path</property>.
  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. <property>include_path</property> for the <acronym>PHP</acronym> environment is
  89. searched.
  90. </para>
  91. <para>
  92. If the file is not found or the class does not exist after the load,
  93. <methodname>Zend_Loader::loadClass()</methodname> throws a
  94. <classname>Zend_Exception</classname>.
  95. </para>
  96. <para>
  97. <methodname>Zend_Loader::loadFile()</methodname> is used for loading, so the
  98. class name may only contain alphanumeric characters and the hyphen
  99. ('-'), underscore ('_'), and period ('.').
  100. </para>
  101. <note>
  102. <title>Loading Classes from PHP Namespaces</title>
  103. <para>
  104. Starting in version 1.10.0, Zend Framework now allows loading classes from
  105. <acronym>PHP</acronym> namespaces. This support follows the same guidelines and
  106. implementation as that found in the <ulink
  107. url="http://groups.google.com/group/php-standards/web/psr-0-final-proposal">PHP
  108. Framework Interop Group PSR-0</ulink> reference implementation.
  109. </para>
  110. <para>
  111. Under this guideline, the following rules apply:
  112. </para>
  113. <itemizedlist>
  114. <listitem>
  115. <para>
  116. Each namespace separator is converted to a
  117. <constant>DIRECTORY_SEPARATOR</constant> when loading from the file system.
  118. </para>
  119. </listitem>
  120. <listitem>
  121. <para>
  122. Each "_" character in the <emphasis>CLASS NAME</emphasis> is converted to a
  123. <constant>DIRECTORY_SEPARATOR</constant>. The "_" character has no special
  124. meaning in the namespace.
  125. </para>
  126. </listitem>
  127. <listitem>
  128. <para>
  129. The fully-qualified namespace and class is suffixed with ".php" when loading
  130. from the file system.
  131. </para>
  132. </listitem>
  133. </itemizedlist>
  134. <para>
  135. As examples:
  136. </para>
  137. <itemizedlist>
  138. <listitem>
  139. <para>
  140. <classname>\Doctrine\Common\IsolatedClassLoader</classname> =&gt;
  141. <filename>/path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php</filename>
  142. </para>
  143. </listitem>
  144. <listitem>
  145. <para>
  146. <classname>\namespace\package\Class_Name</classname> =&gt;
  147. <filename>/path/to/project/lib/vendor/namespace/package/Class/Name.php</filename>
  148. </para>
  149. </listitem>
  150. <listitem>
  151. <para>
  152. <classname>\namespace\package_name\Class_Name</classname> =&gt;
  153. <filename>/path/to/project/lib/vendor/namespace/package_name/Class/Name.php</filename>
  154. </para>
  155. </listitem>
  156. </itemizedlist>
  157. </note>
  158. </sect2>
  159. <sect2 id="zend.loader.load.isreadable">
  160. <title>Testing if a File is Readable</title>
  161. <para>
  162. The static method <methodname>Zend_Loader::isReadable($pathname)</methodname>
  163. returns <constant>TRUE</constant> if a file at the specified pathname exists
  164. and is readable, <constant>FALSE</constant> otherwise.
  165. </para>
  166. <example id="zend.loader.load.isreadable.example">
  167. <title>Example of isReadable() method</title>
  168. <programlisting language="php"><![CDATA[
  169. if (Zend_Loader::isReadable($filename)) {
  170. // do something with $filename
  171. }
  172. ]]></programlisting>
  173. </example>
  174. <para>
  175. The <varname>$filename</varname> argument specifies the filename to
  176. check. This may contain path information.
  177. This method is a wrapper for the <acronym>PHP</acronym> function
  178. <ulink url="http://php.net/is_readable"><methodname>is_readable()</methodname></ulink>.
  179. The <acronym>PHP</acronym> function does not search the
  180. <property>include_path</property>, while
  181. <methodname>Zend_Loader::isReadable()</methodname> does.
  182. </para>
  183. </sect2>
  184. <sect2 id="zend.loader.load.autoload">
  185. <title>Using the Autoloader</title>
  186. <para>
  187. The <classname>Zend_Loader</classname> class contains a method you can register with the
  188. <acronym>PHP</acronym> SPL autoloader. <methodname>Zend_Loader::autoload()</methodname>
  189. is the callback method. As a convenience, <classname>Zend_Loader</classname> provides
  190. the <methodname>registerAutoload()</methodname> function to register its
  191. <methodname>autoload()</methodname> method. If the <property>spl_autoload</property>
  192. extension is not present in your <acronym>PHP</acronym> environment, then the
  193. <methodname>registerAutoload()</methodname> method throws a
  194. <classname>Zend_Exception</classname>.
  195. </para>
  196. <example id="zend.loader.load.autoload.example">
  197. <title>Example of registering the autoloader callback method</title>
  198. <programlisting language="php"><![CDATA[
  199. Zend_Loader::registerAutoload();
  200. ]]></programlisting>
  201. </example>
  202. <para>
  203. After registering the Zend Framework autoload callback, you can
  204. reference classes from Zend Framework without having to load
  205. them explicitly. The <methodname>autoload()</methodname> method uses
  206. <methodname>Zend_Loader::loadClass()</methodname> automatically when you
  207. reference a class.
  208. </para>
  209. <para>
  210. If you have extended the <classname>Zend_Loader</classname> class, you can give an
  211. optional argument to <methodname>registerAutoload()</methodname>, to specify
  212. the class from which to register an <methodname>autoload()</methodname> method.
  213. </para>
  214. <example id="zend.loader.load.autoload.example-extended">
  215. <title>Example of registering the autoload callback method from an
  216. extended class</title>
  217. <para>
  218. Because of the semantics of static function references in <acronym>PHP</acronym>,
  219. you must implement code for both <methodname>loadClass()</methodname>
  220. and <methodname>autoload()</methodname>, and the <methodname>autoload()</methodname>
  221. must call <methodname>self::loadClass()</methodname>. If your
  222. <methodname>autoload()</methodname> method delegates to its parent to
  223. call <methodname>self::loadClass()</methodname>, then it calls the
  224. method of that name in the parent class, not the subclass.
  225. </para>
  226. <programlisting language="php"><![CDATA[
  227. class My_Loader extends Zend_Loader
  228. {
  229. public static function loadClass($class, $dirs = null)
  230. {
  231. parent::loadClass($class, $dirs);
  232. }
  233. public static function autoload($class)
  234. {
  235. try {
  236. self::loadClass($class);
  237. return $class;
  238. } catch (Exception $e) {
  239. return false;
  240. }
  241. }
  242. }
  243. Zend_Loader::registerAutoload('My_Loader');
  244. ]]></programlisting>
  245. </example>
  246. <para>
  247. You can remove an autoload callback. The
  248. <methodname>registerAutoload()</methodname> has an optional second argument,
  249. which is <constant>TRUE</constant> by default. If this argument is
  250. <constant>FALSE</constant>, the autoload callback is unregistered from the
  251. SPL autoload stack.
  252. </para>
  253. </sect2>
  254. </sect1>
  255. <!--
  256. vim:se ts=4 sw=4 et:
  257. -->