This article mainly introduces the Zend_Registry object usage in the ZendFramework tutorial, and analyzes the specific functions and usage skills of the Object Registry Zend_Registry based on the instance form, for more information, see 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 );