Zend_Loader.xml 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. </sect2>
  97. <sect2 id="zend.loader.load.isreadable">
  98. <title>Testing if a File is Readable</title>
  99. <para>
  100. The static method <methodname>Zend_Loader::isReadable($pathname)</methodname>
  101. returns <constant>TRUE</constant> if a file at the specified pathname exists
  102. and is readable, <constant>FALSE</constant> otherwise.
  103. </para>
  104. <example id="zend.loader.load.isreadable.example">
  105. <title>Example of isReadable() method</title>
  106. <programlisting language="php"><![CDATA[
  107. if (Zend_Loader::isReadable($filename)) {
  108. // do something with $filename
  109. }
  110. ]]></programlisting>
  111. </example>
  112. <para>
  113. The <varname>$filename</varname> argument specifies the filename to
  114. check. This may contain path information.
  115. This method is a wrapper for the <acronym>PHP</acronym> function
  116. <ulink url="http://php.net/is_readable"><methodname>is_readable()</methodname></ulink>.
  117. The <acronym>PHP</acronym> function does not search the <code>include_path</code>,
  118. while <methodname>Zend_Loader::isReadable()</methodname> does.
  119. </para>
  120. </sect2>
  121. <sect2 id="zend.loader.load.autoload">
  122. <title>Using the Autoloader</title>
  123. <para>
  124. The <classname>Zend_Loader</classname> class contains a method you can register with the
  125. <acronym>PHP</acronym> SPL autoloader. <methodname>Zend_Loader::autoload()</methodname> is the
  126. callback method. As a convenience, <classname>Zend_Loader</classname> provides the
  127. <methodname>registerAutoload()</methodname> function to register its
  128. <methodname>autoload()</methodname> method. If the <code>spl_autoload</code>
  129. extension is not present in your <acronym>PHP</acronym> environment, then the
  130. <methodname>registerAutoload()</methodname> method throws a <classname>Zend_Exception</classname>.
  131. </para>
  132. <example id="zend.loader.load.autoload.example">
  133. <title>Example of registering the autoloader callback method</title>
  134. <programlisting language="php"><![CDATA[
  135. Zend_Loader::registerAutoload();
  136. ]]></programlisting>
  137. </example>
  138. <para>
  139. After registering the Zend Framework autoload callback, you can
  140. reference classes from Zend Framework without having to load
  141. them explicitly. The <methodname>autoload()</methodname> method uses
  142. <methodname>Zend_Loader::loadClass()</methodname> automatically when you
  143. reference a class.
  144. </para>
  145. <para>
  146. If you have extended the <classname>Zend_Loader</classname> class, you can give an
  147. optional argument to <methodname>registerAutoload()</methodname>, to specify
  148. the class from which to register an <methodname>autoload()</methodname> method.
  149. </para>
  150. <example id="zend.loader.load.autoload.example-extended">
  151. <title>Example of registering the autoload callback method from an
  152. extended class</title>
  153. <para>
  154. Because of the semantics of static function references in <acronym>PHP</acronym>,
  155. you must implement code for both <methodname>loadClass()</methodname>
  156. and <methodname>autoload()</methodname>, and the <methodname>autoload()</methodname>
  157. must call <methodname>self::loadClass()</methodname>. If your
  158. <methodname>autoload()</methodname> method delegates to its parent to
  159. call <methodname>self::loadClass()</methodname>, then it calls the
  160. method of that name in the parent class, not the subclass.
  161. </para>
  162. <programlisting language="php"><![CDATA[
  163. class My_Loader extends Zend_Loader
  164. {
  165. public static function loadClass($class, $dirs = null)
  166. {
  167. parent::loadClass($class, $dirs);
  168. }
  169. public static function autoload($class)
  170. {
  171. try {
  172. self::loadClass($class);
  173. return $class;
  174. } catch (Exception $e) {
  175. return false;
  176. }
  177. }
  178. }
  179. Zend_Loader::registerAutoload('My_Loader');
  180. ]]></programlisting>
  181. </example>
  182. <para>
  183. You can remove an autoload callback. The
  184. <methodname>registerAutoload()</methodname> has an optional second argument,
  185. which is <constant>TRUE</constant> by default. If this argument is
  186. <constant>FALSE</constant>, the autoload callback is unregistered from the
  187. SPL autoload stack.
  188. </para>
  189. </sect2>
  190. </sect1>
  191. <!--
  192. vim:se ts=4 sw=4 et:
  193. -->