File: Zend_Registry.xml

package info (click to toggle)
zendframework 1.12.9%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 133,584 kB
  • sloc: xml: 1,311,829; php: 570,173; sh: 170; makefile: 125; sql: 121
file content (336 lines) | stat: -rw-r--r-- 11,694 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?xml version="1.0" encoding="UTF-8"?>
<!-- Reviewed: no -->
<sect1 id="zend.registry.using">
    <title>Using the Registry</title>

    <para>
        A registry is a container for storing objects and values in the
        application space. By storing the value in a registry, the same
        object is always available throughout your application.
        This mechanism is an alternative to using global storage.
    </para>

    <para>
        The typical method to use registries with Zend Framework is through static methods in the
        <classname>Zend_Registry</classname> class. Alternatively, the registry can be used as an
        array object, so you can access elements stored within it with a convenient array-like
        interface.
    </para>

    <sect2 id="zend.registry.using.storing">
        <title>Setting Values in the Registry</title>

        <para>
            Use the static method <methodname>set()</methodname> to store an entry in the registry.
        </para>

        <example id="zend.registry.using.storing.example">
            <title>Example of set() Method Usage</title>

            <programlisting language="php"><![CDATA[
Zend_Registry::set('index', $value);
]]></programlisting>
        </example>

        <para>
            The value returned can be an object, an array, or a scalar.
            You can change the value stored in a specific entry of
            the registry by calling the <methodname>set()</methodname> method to set the entry
            to a new value.
        </para>

        <para>
            The index can be a scalar (<constant>NULL</constant>, string, or number), like an
            ordinary array.
        </para>
    </sect2>

    <sect2 id="zend.registry.using.retrieving">
        <title>Getting Values from the Registry</title>

        <para>
            To retrieve an entry from the registry, use the static
            <methodname>get()</methodname> method.
        </para>

        <example id="zend.registry.using.retrieving.example">
            <title>Example of get() Method Usage</title>

            <programlisting language="php"><![CDATA[
$value = Zend_Registry::get('index');
]]></programlisting>
        </example>

        <para>
            The <methodname>getInstance()</methodname> method returns the singleton registry object.
            This registry object is iterable, making all values stored in the registry easily
            accessible.
        </para>

        <example id="zend.registry.using.retrieving.example-iterating">
            <title>Example of Iterating over the Registry</title>

            <programlisting language="php"><![CDATA[
$registry = Zend_Registry::getInstance();

foreach ($registry as $index => $value) {
    echo "Registry index $index contains:\n";
    var_dump($value);
}
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.registry.using.constructing">
        <title>Constructing a Registry Object</title>

        <para>
            In addition to accessing the static registry via
            static methods, you can create an instance directly and
            use it as an object.
        </para>

        <para>
            The registry instance you access through the
            static methods is simply one such instance. It is
            for convenience that it is stored statically, so that it is
            accessible from anywhere in an application.
        </para>

        <para>
            Use the traditional <emphasis>new</emphasis> operator to instantiate
            <classname>Zend_Registry</classname>. Instantiating <classname>Zend_Registry</classname>
            using its constructor also makes initializing the entries in the registry simple by
            taking an associative array as an argument.
        </para>

        <example id="zend.registry.using.constructing.example">
            <title>Example of Constructing a Registry</title>

            <programlisting language="php"><![CDATA[
$registry = new Zend_Registry(array('index' => $value));
]]></programlisting>
        </example>

        <para>
            Once such a <classname>Zend_Registry</classname> object is instantiated,
            you can use it by calling any array object method or by setting it
            as the singleton instance for <classname>Zend_Registry</classname> with the static
            method <methodname>setInstance()</methodname>.
        </para>

        <example id="zend.registry.using.constructing.example-setinstance">
            <title>Example of Initializing the Singleton Registry</title>

            <programlisting language="php"><![CDATA[
$registry = new Zend_Registry(array('index' => $value));

Zend_Registry::setInstance($registry);
]]></programlisting>
        </example>

        <para>
            The <methodname>setInstance()</methodname> method throws a
            <classname>Zend_Exception</classname> if the static registry has already been
            initialized.
        </para>
    </sect2>

    <sect2 id="zend.registry.using.array-access">
        <title>Accessing the Registry as an Array</title>

        <para>
            If you have several values to get or set, you may find it
            convenient to access the registry with array notation.
        </para>

        <example id="zend.registry.using.array-access.example">
            <title>Example of Array Access</title>

            <programlisting language="php"><![CDATA[
$registry = Zend_Registry::getInstance();

$registry['index'] = $value;

var_dump( $registry['index'] );
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.registry.using.array-object">
        <title>Accessing the Registry as an Object</title>

        <para>
            You may also find it convenient to access the registry
            in an object-oriented fashion by using index names as object
            properties.
            You must specifically construct the registry
            object using the <constant>ArrayObject::ARRAY_AS_PROPS</constant> option
            and initialize the static instance to enable this functionality.

            <note>
                <para>You must set the <constant>ArrayObject::ARRAY_AS_PROPS</constant> option
                <emphasis>before</emphasis> the static registry has been accessed for
                the first time.</para>
            </note>
        </para>

        <warning>
            <title>Known Issues with the ArrayObject::ARRAY_AS_PROPS Option</title>

            <para>
                Some versions of <acronym>PHP</acronym> have proven very buggy when using the
                registry with the <constant>ArrayObject::ARRAY_AS_PROPS</constant> option.
            </para>
        </warning>

        <example id="zend.registry.using.array-object.example">
            <title>Example of Object Access</title>

            <programlisting language="php"><![CDATA[
// in your application bootstrap:
$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS)
Zend_Registry::setInstance($registry);
$registry->tree = 'apple';

.
.
.

// in a different function, elsewhere in your application:
$registry = Zend_Registry::getInstance();

echo $registry->tree; // echo's "apple"

$registry->index = $value;

var_dump($registry->index);
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.registry.using.isset">
        <title>Querying if an Index Exists</title>

        <para>
            To find out if a particular index in the registry
            has been set, use the static method <methodname>isRegistered()</methodname>.
        </para>

        <example id="zend.registry.using.isset.example-isregistered">
            <title>Example of isRegistered() Method Usage</title>

            <programlisting language="php"><![CDATA[
if (Zend_Registry::isRegistered($index)) {
    $value = Zend_Registry::get($index);
}
]]></programlisting>
        </example>

        <para>
            To find out if a particular index in a registry
            array or object has a value, use the <methodname>isset()</methodname> function
            as you would with an ordinary array.
        </para>

        <example id="zend.registry.using.isset.example-isset">
            <title>Example of isset() Method Usage</title>

            <programlisting language="php"><![CDATA[
$registry = Zend_Registry::getInstance();

// using array access syntax
if (isset($registry['index'])) {
    var_dump( $registry['index'] );
}

// using object access syntax
if (isset($registry->index)) {
    var_dump( $registry->index );
}
]]></programlisting>
        </example>
    </sect2>

    <sect2 id="zend.registry.using.subclassing">
        <title>Extending the Registry</title>

        <para>
            The static registry is an instance of the class <classname>Zend_Registry</classname>.
            If you want to add functionality to the registry, you should
            create a class that extends <classname>Zend_Registry</classname> and
            specify this class to instantiate for the singleton in the static registry.
            Use the static method <methodname>setClassName()</methodname> to specify
            the class.

            <note>
                <para>The class must be a subclass of <classname>Zend_Registry</classname>.</para>
            </note>
        </para>

        <example id="zend.registry.using.subclassing.example">
            <title>Example of Specifying the Singleton Registry's Class Name</title>

            <programlisting language="php"><![CDATA[
Zend_Registry::setClassName('My_Registry');

Zend_Registry::set('index', $value);
]]></programlisting>
        </example>

        <para>
            The registry throws a <classname>Zend_Exception</classname> if you attempt to set the
            classname after the registry has been accessed for the first time.
            It is therefore recommended that you specify the class name for your
            static registry in your application bootstrap.
        </para>
    </sect2>

    <sect2 id="zend.registry.using.unsetting">
        <title>Unsetting the Static Registry</title>

        <para>
            Although it is not normally necessary, you can
            unset the singleton instance of the registry, if desired.
            Use the static method <methodname>_unsetInstance()</methodname> to do so.
        </para>

        <warning>
            <title>Data Loss Risk</title>

            <para>
                When you use <methodname>_unsetInstance()</methodname>,
                all data in the static registry are
                discarded and cannot be recovered.
            </para>
        </warning>

        <para>
            You might use this method, for example, if you want to
            use <methodname>setInstance()</methodname> or <methodname>setClassName()</methodname>
            after the singleton registry object has been initialized.
            Unsetting the singleton instance allows you to use these methods
            even after the singleton registry object has been set. Using
            <classname>Zend_Registry</classname> in this manner is not recommended for typical
            applications and environments.
        </para>

        <example id="zend.registry.using.unsetting.example">
            <title>Example of _unsetInstance() Method Usage</title>

            <programlisting language="php"><![CDATA[
Zend_Registry::set('index', $value);

Zend_Registry::_unsetInstance();

// change the class
Zend_Registry::setClassName('My_Registry');

Zend_Registry::set('index', $value);
]]></programlisting>
        </example>
    </sect2>
</sect1>
<!--
vim:se ts=4 sw=4 et:
-->