Zend_Registry.xml 11 KB

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