This example describes the Zend_Registry object usage in the ZendFramework tutorial. For your reference, the object Registry (or object repository) is) the container that stores objects and values in the container. By storing objects in it, we can
This example describes the Zend_Registry object usage in the Zend Framework tutorial. The object Registry (or object repository) is an application space) the container that stores objects and values in the container. By storing objects in it, we can
This example describes the Zend_Registry object usage in the Zend Framework tutorial. We will share this with you for your reference. The details are as follows:
Use the object Registry)
The object registry (or object repository) is a container used to store objects and values in the entire application space. By storing objects in it, we can use the same object anywhere in the entire project. This mechanism is equivalent to a global storage.
We can use the static method of the Zend_Registry class to use the object Registry. In addition, because this class is an array object, you can access the class method in the array form.
1. Set the value in Registry
To save a content to the Registry, we can use the static method set ().
Example 1. set () Example:
Zend_Registry::set('index', $value);
$ Value can be an object, array, or scalar. You can use set () to set a new value for an existing value in the registry.
The index parameter can be a scalar, that is, a string or integer, just like an array, similar to the index/key name of an array.
2. Get the value in Registry
You can use the get () method to obtain the value of a certain item in the Registry.
Example 2. get () method example:
$value = Zend_Registry::get('index');
GetInstance () returns the static registry object.
Registry objects are iteratable ).
Example 3. iterate a registry object:
$registry = Zend_Registry::getInstance();foreach ($registry as $index => $value) { echo "Registry index $index contains:/n"; var_dump($value);}
3. Create a Registry object
In addition to using static methods to access a Registry object, you can instantiate it directly, just like using a common object.
If you use a static method to access the registry object instance, it facilitates static storage and you can access it anywhere in the program.
If you use the traditional new method to create a registry instance, you can initialize the content in the registry in the same way as the array.
Example 4. Create a registry object
$registry = new Zend_Registry(array('index' => $value));
After creating this object instance, you can use the array object method, or you can set this object instance to a static object instance through the static method setInstance.
Example 5: Example of initializing the static registry
$registry = new Zend_Registry(array('index' => $value));Zend_Registry::setInstance($registry);
If the static registry object has been initialized, The setInstance () method throws a Zend_Exception.
4. Access the Registry object like an array
If you want to access or set multiple values at a time, you will find it convenient to use arrays.
Example 6: array access example:
$registry = Zend_Registry::getInstance();$registry['index'] = $value;var_dump( $registry['index'] );
5. Access Registry through objects
You will find that it is convenient to access the registry object using the object-oriented style, and the attribute name in the object is used as the index. To do this, you need to use the ArrayObject: ARRAY_AS_PROPS option to create a registry object and initialize a static instance. You need to complete the work before the static registry is accessed for the first time. Be careful when using this option, because some versions of PHP may have bugs when using this option.
Example 7. Object Access:
// In Your bootstrap code: $ registry = new Zend_Registry (array (), ArrayObject: ARRAY_AS_PROPS) Zend_Registry: setInstance ($ registry ); $ registry-> tree = 'apple ';... // elsewhere in the program: $ registry = Zend_Registry: getInstance (); echo $ registry-> tree; // echo's "apple" $ registry-> index = $ value; var_dump ($ registry-> index );
6. Check whether an index exists.
You can use the static isRegistered () method to check whether a specific index has set the corresponding value.
Example 8. isRegistered:
if (Zend_Registry::isRegistered($index)) { $value = Zend_Registry::get($index);}
To determine whether the value of a specific index in an array object is set, you can use the isset () function, just as in an ordinary array.
Example 9. isset:
$registry = Zend_Registry::getInstance();// using array-access syntaxif (isset($registry['index'])) { var_dump( $registry['index'] );}// using object-access syntax, if enabledif (isset($registry->index)) { var_dump( $registry->index );}
7. Extend the Registry object
The static registry object is an instance of the Zend_Registry class. If you want to add a function to it, you can inherit the Zend_Registry class and specify to use this class to access the object registry. You can use the static method setClassName () to specify the class. Note that this class must be a subclass of Zend_Registry.
Example 10. Specify the Class Name of the static registry:
Zend_Registry::setClassName('My_Registry');Zend_Registry::set('index', $value);
If you try to set the class name after the registry has been accessed, the registry throws an exception. We recommend that you set the class name in the boostrap code (index. php.
8. Delete the static Registry
Although this is not necessary, you can use the _ unsetInstance () method to delete the static instance of the registry.
[Note] risks of data loss
When _ unsetInstance () is used, all data in the static registry is lost and cannot be recovered.
Sometimes you may need the _ unsetInstance () method. For example, if you want to use setInstance () or setClassName () after the registry object has been initialized, you can use _ unsetInstance () to delete the static instance first, then you can use those methods.
Example 11. _ unsetInstance:
Zend_Registry: set ('index', $ value); Zend_Registry: _ unsetInstance (); // change the class Zend_Registry :: setClassName ('My _ Registry '); Zend_Registry: set ('index', $ value );