Zend_Registry.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.registry.using">
  4. <title>Using the Registry</title>
  5. <para>
  6. A registry is a container for storing objects and values in the
  7. application space. By storing the value in a registry, the same
  8. object is always available throughout your application.
  9. This mechanism is an alternative to using global storage.
  10. </para>
  11. <para>
  12. The typical method to use registries with Zend Framework is through static methods in the
  13. <classname>Zend_Registry</classname> class. Alternatively, the registry can be used as an array object,
  14. so you can access elements stored within it with a convenient array-like interface.
  15. </para>
  16. <sect2 id="zend.registry.using.storing">
  17. <title>Setting Values in the Registry</title>
  18. <para>
  19. Use the static method <code>set()</code> to store an entry in the registry, .
  20. </para>
  21. <example id="zend.registry.using.storing.example">
  22. <title>Example of set() Method Usage</title>
  23. <programlisting language="php"><![CDATA[
  24. Zend_Registry::set('index', $value);
  25. ]]></programlisting>
  26. </example>
  27. <para>
  28. The value returned can be an object, an array, or a scalar.
  29. You can change the value stored in a specific entry of
  30. the registry by calling the <code>set()</code> method to set the entry
  31. to a new value.
  32. </para>
  33. <para>
  34. The index can be a scalar (<constant>NULL</constant>, string, or number), like an ordinary array.
  35. </para>
  36. </sect2>
  37. <sect2 id="zend.registry.using.retrieving">
  38. <title>Getting Values from the Registry</title>
  39. <para>
  40. To retrieve an entry from the registry, use the static
  41. <code>get()</code> method.
  42. </para>
  43. <example id="zend.registry.using.retrieving.example">
  44. <title>Example of get() Method Usage</title>
  45. <programlisting language="php"><![CDATA[
  46. $value = Zend_Registry::get('index');
  47. ]]></programlisting>
  48. </example>
  49. <para>
  50. The <code>getInstance()</code> method returns the singleton registry object.
  51. This registry object is iterable, making all values stored in the registry easily accessible.
  52. </para>
  53. <example id="zend.registry.using.retrieving.example-iterating">
  54. <title>Example of Iterating over the Registry</title>
  55. <programlisting language="php"><![CDATA[
  56. $registry = Zend_Registry::getInstance();
  57. foreach ($registry as $index => $value) {
  58. echo "Registry index $index contains:\n";
  59. var_dump($value);
  60. }
  61. ]]></programlisting>
  62. </example>
  63. </sect2>
  64. <sect2 id="zend.registry.using.constructing">
  65. <title>Constructing a Registry Object</title>
  66. <para>
  67. In addition to accessing the static registry via
  68. static methods, you can create an instance directly and
  69. use it as an object.
  70. </para>
  71. <para>
  72. The registry instance you access through the
  73. static methods is simply one such instance. It is
  74. for convenience that it is stored statically, so that it is
  75. accessible from anywhere in an application.
  76. </para>
  77. <para>
  78. Use the traditional <code>new</code> operator to instantiate
  79. <classname>Zend_Registry</classname>. Instantiating <classname>Zend_Registry</classname>
  80. using its constructor also makes initializing the entries in the registry simple by
  81. taking an associative array as an argument.
  82. </para>
  83. <example id="zend.registry.using.constructing.example">
  84. <title>Example of Constructing a Registry</title>
  85. <programlisting language="php"><![CDATA[
  86. $registry = new Zend_Registry(array('index' => $value));
  87. ]]></programlisting>
  88. </example>
  89. <para>
  90. Once such a <classname>Zend_Registry</classname> object is instantiated,
  91. you can use it by calling any array object method or by setting it
  92. as the singleton instance for <classname>Zend_Registry</classname> with the static method
  93. <code>setInstance()</code>.
  94. </para>
  95. <example id="zend.registry.using.constructing.example-setinstance">
  96. <title>Example of Initializing the Singleton Registry</title>
  97. <programlisting language="php"><![CDATA[
  98. $registry = new Zend_Registry(array('index' => $value));
  99. Zend_Registry::setInstance($registry);
  100. ]]></programlisting>
  101. </example>
  102. <para>
  103. The <code>setInstance()</code> method throws a <classname>Zend_Exception</classname>
  104. if the static registry has already been initialized.
  105. </para>
  106. </sect2>
  107. <sect2 id="zend.registry.using.array-access">
  108. <title>Accessing the Registry as an Array</title>
  109. <para>
  110. If you have several values to get or set, you may find it
  111. convenient to access the registry with array notation.
  112. </para>
  113. <example id="zend.registry.using.array-access.example">
  114. <title>Example of Array Access</title>
  115. <programlisting language="php"><![CDATA[
  116. $registry = Zend_Registry::getInstance();
  117. $registry['index'] = $value;
  118. var_dump( $registry['index'] );
  119. ]]></programlisting>
  120. </example>
  121. </sect2>
  122. <sect2 id="zend.registry.using.array-object">
  123. <title>Accessing the Registry as an Object</title>
  124. <para>
  125. You may also find it convenient to access the registry
  126. in an object-oriented fashion by using index names as object
  127. properties.
  128. You must specifically construct the registry
  129. object using the <code>ArrayObject::ARRAY_AS_PROPS</code> option
  130. and initialize the static instance to enable this functionality.
  131. <note>
  132. <para>You must set the <code>ArrayObject::ARRAY_AS_PROPS</code> option
  133. <emphasis>before</emphasis> the static registry has been accessed for
  134. the first time.</para>
  135. </note>
  136. </para>
  137. <warning>
  138. <title>Known Issues with the ArrayObject::ARRAY_AS_PROPS Option</title>
  139. <para>
  140. Some versions of PHP have proven very buggy when using the registry with the <code>ArrayObject::ARRAY_AS_PROPS</code> option.
  141. </para>
  142. </warning>
  143. <example id="zend.registry.using.array-object.example">
  144. <title>Example of Object Access</title>
  145. <programlisting language="php"><![CDATA[
  146. // in your application bootstrap:
  147. $registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS)
  148. Zend_Registry::setInstance($registry);
  149. $registry->tree = 'apple';
  150. .
  151. .
  152. .
  153. // in a different function, elsewhere in your application:
  154. $registry = Zend_Registry::getInstance();
  155. echo $registry->tree; // echo's "apple"
  156. $registry->index = $value;
  157. var_dump($registry->index);
  158. ]]></programlisting>
  159. </example>
  160. </sect2>
  161. <sect2 id="zend.registry.using.isset">
  162. <title>Querying if an Index Exists</title>
  163. <para>
  164. To find out if a particular index in the registry
  165. has been set, use the static method <code>isRegistered()</code>.
  166. </para>
  167. <example id="zend.registry.using.isset.example-isregistered">
  168. <title>Example of isRegistered() Method Usage</title>
  169. <programlisting language="php"><![CDATA[
  170. if (Zend_Registry::isRegistered($index)) {
  171. $value = Zend_Registry::get($index);
  172. }
  173. ]]></programlisting>
  174. </example>
  175. <para>
  176. To find out if a particular index in a registry
  177. array or object has a value, use the <code>isset()</code> function
  178. as you would with an ordinary array.
  179. </para>
  180. <example id="zend.registry.using.isset.example-isset">
  181. <title>Example of isset() Method Usage</title>
  182. <programlisting language="php"><![CDATA[
  183. $registry = Zend_Registry::getInstance();
  184. // using array access syntax
  185. if (isset($registry['index'])) {
  186. var_dump( $registry['index'] );
  187. }
  188. // using object access syntax
  189. if (isset($registry->index)) {
  190. var_dump( $registry->index );
  191. }
  192. ]]></programlisting>
  193. </example>
  194. </sect2>
  195. <sect2 id="zend.registry.using.subclassing">
  196. <title>Extending the Registry</title>
  197. <para>
  198. The static registry is an instance of the class <classname>Zend_Registry</classname>.
  199. If you want to add functionality to the registry, you should
  200. create a class that extends <classname>Zend_Registry</classname> and
  201. specify this class to instantiate for the singleton in the static registry.
  202. Use the static method <code>setClassName()</code> to specify
  203. the class.
  204. <note>
  205. <para>The class must be a subclass of <classname>Zend_Registry</classname>.</para>
  206. </note>
  207. </para>
  208. <example id="zend.registry.using.subclassing.example">
  209. <title>Example of Specifying the Singleton Registry's Class Name</title>
  210. <programlisting language="php"><![CDATA[
  211. Zend_Registry::setClassName('My_Registry');
  212. Zend_Registry::set('index', $value);
  213. ]]></programlisting>
  214. </example>
  215. <para>
  216. The registry throws a <classname>Zend_Exception</classname> if you attempt to set the
  217. classname after the registry has been accessed for the first time.
  218. It is therefore recommended that you specify the class name for your
  219. static registry in your application bootstrap.
  220. </para>
  221. </sect2>
  222. <sect2 id="zend.registry.using.unsetting">
  223. <title>Unsetting the Static Registry</title>
  224. <para>
  225. Although it is not normally necessary, you can
  226. unset the singleton instance of the registry, if desired.
  227. Use the static method <code>_unsetInstance()</code> to do so.
  228. </para>
  229. <warning>
  230. <title>Data Loss Risk</title>
  231. <para>
  232. When you use <code>_unsetInstance()</code>,
  233. all data in the static registry are
  234. discarded and cannot be recovered.
  235. </para>
  236. </warning>
  237. <para>
  238. You might use this method, for example, if you want to
  239. use <code>setInstance()</code> or <code>setClassName()</code>
  240. after the singleton registry object has been initialized.
  241. Unsetting the singleton instance allows you to use these methods
  242. even after the singleton registry object has been set. Using
  243. <classname>Zend_Registry</classname> in this manner is not recommended for typical
  244. applications and environments.
  245. </para>
  246. <example id="zend.registry.using.unsetting.example">
  247. <title>Example of _unsetInstance() Method Usage</title>
  248. <programlisting language="php"><![CDATA[
  249. Zend_Registry::set('index', $value);
  250. Zend_Registry::_unsetInstance();
  251. // change the class
  252. Zend_Registry::setClassName('My_Registry');
  253. Zend_Registry::set('index', $value);
  254. ]]></programlisting>
  255. </example>
  256. </sect2>
  257. </sect1>
  258. <!--
  259. vim:se ts=4 sw=4 et:
  260. -->