Home > Backend Development > PHP Tutorial > Zend Framework tutorial Zend_Registry object usage analysis, zendzend_registry_PHP tutorial

Zend Framework tutorial Zend_Registry object usage analysis, zendzend_registry_PHP tutorial

WBOY
Release: 2016-07-12 08:56:09
Original
905 people have browsed it

Zend Framework tutorial Zend_Registry object usage analysis, zendzend_registry

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);

Copy after login

$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');

Copy after login

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);
}

Copy after login

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));

Copy after login

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);

Copy after login

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'] );

Copy after login

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);

Copy after login

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);
}

Copy after login

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 );
}

Copy after login

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);

Copy after login

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);

Copy after login

更多关于zend相关内容感兴趣的读者可查看本站专题:《Zend FrameWork框架入门教程》、《php优秀开发框架总结》、《Yii框架入门及常用技巧总结》、《ThinkPHP入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Zend 输出产生XML解析错误
  • 基于Zend的Config机制的应用分析
  • Zend Framework实现多服务器共享SESSION数据的方法
  • Zend Framework框架Smarty扩展实现方法
  • Zend Framework框架路由机制代码分析
  • Zend Framework实现留言本分页功能(附demo源码下载)
  • Zend Framework实现将session存储在memcache中的方法
  • Zend Framework分页类用法详解
  • Zend Framework生成验证码并实现验证码验证功能(附demo源码下载)
  • Zend Framework实现多文件上传功能实例
  • Zend Framework入门之环境配置及第一个Hello World示例(附demo源码下载)
  • Zend Framework入门知识点小结
  • Zend Framework教程之Zend_Config_Xml用法分析

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1113716.htmlTechArticleZend Framework教程之Zend_Registry对象用法分析,zendzend_registry 本文实例讲述了Zend Framework教程之Zend_Registry对象用法。分享给大家供大家参考,具...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template