This article describes the Zend Framework tutorial Zend_Registry object usage with examples. Share it with everyone for your reference, the details are as follows:
Use object registry (Registry)
The object registry (or object warehouse) is a container used to store objects and values throughout the application space. By storing objects in it, we can use the same object anywhere throughout the project. This mechanism is equivalent to a global storage.
We can use the object registry through the static methods of the Zend_Registry class. In addition, since this class is an array object, you can use the array form to access the class methods.
1. Set the value in Registry
To save an item to the registry, we can use the static method set().
Example 1. Set() usage example:
Zend_Registry::set('index', $value);
$value can be an object, array or scalar. You can use set() again to set a new value to a value already in the registry.
The index parameter can be a scalar, a string or an integer, just like using an array, similar to the index/key name of the array.
2. Get the value in Registry
You can use the get() method to get the value of an item in the Registry.
Example 2. get() method example:
$value = Zend_Registry::get('index');
getInstance() returns a static registry object.
Registry objects are iterable.
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 the Registry object, you can instantiate it directly, just like using a normal object.
If you access the instance of the registry object through a static method, it is convenient for static storage, and you can access it anywhere in the program.
If you use the traditional new method to create an instance of the registry, you can use an array to initialize the contents of the registry.
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 to use it, 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 will throw a Zend_Exception.
4. Access the Registry object like an array
If you want to access or set multiple values at once, you will find it convenient to use the array method.
Example 6. Array access example:
$registry = Zend_Registry::getInstance(); $registry['index'] = $value; var_dump( $registry['index'] );
5. Access the Registry in object mode
You will find it convenient to access registry objects using an object-oriented style, using the property names in the object as indexes. To do this, you need to create the registry object using the ArrayObject::ARRAY_AS_PROPS option and initialize the static instance. You need to do this before the static registry is accessed for the first time. Be careful with this option, as some versions of PHP have bugs when using this option.
Example 7. Access in object form:
//在你的bootstrap代码中: $registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS) Zend_Registry::setInstance($registry); $registry->tree = 'apple'; . . . //在程序的任何其它地方: $registry = Zend_Registry::getInstance(); echo $registry->tree; // echo's "apple" $registry->index = $value; var_dump($registry->index);
6. Query whether an index exists
You can use the static method isRegistered() to query whether a specific index has a corresponding value set.
Example 8. isRegistered() Example:
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 like you would use it in a normal array.
Example 9. isset() Example:
$registry = Zend_Registry::getInstance(); // using array-access syntax if (isset($registry['index'])) { var_dump( $registry['index'] ); } // using object-access syntax, if enabled if (isset($registry->index)) { var_dump( $registry->index ); }
7. Extend Registry object
Static registry object is an instance of class Zend_Registry. If you want to add functionality to it, you can inherit the Zend_Registry class and then specify to use this class to access the object registry. You can use the static method setClassName() to specify the class to use. 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. It is recommended that you set this class name in the boostrap code (i.e. index.php).
8. Delete static registry
Although this is not required, you can use the _unsetInstance() method to delete a static instance of the registry.
[Note] Risk of data loss
When using _unsetInstance(), all data in the static registry will be 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 before using those methods.
Example 11. _unsetInstance() Example:
Zend_Registry::set('index', $value); Zend_Registry::_unsetInstance(); // 改变我们要使用的类 Zend_Registry::setClassName('My_Registry'); Zend_Registry::set('index', $value);
更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。