Home >php教程 >PHP开发 >Zend Framework tutorial Zend_Registry object usage analysis

Zend Framework tutorial Zend_Registry object usage analysis

高洛峰
高洛峰Original
2016-12-26 15:35:151084browse

The example in this article describes the usage of the Zend_Registry object in the Zend Framework tutorial. Share it with everyone for your reference, the details are as follows:

Using the Object Registry (Registry)

The Object Registry (or object warehouse) is an object used in the entire application space (application space) A container for storing objects and values. 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, i.e. a string or an integer, just like using an array, similar to the index/key name of the array.

2. Get the value in the 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 over 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 In addition to accessing 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 initialize the contents of the registry in the same way as an 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 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 A Zend_Exception will be thrown.

4. Access the Registry object like an array

If you want to access or set multiple values ​​at one time, you will find that it is very convenient to use the array method.

Example 6. Array method access example:

$registry = Zend_Registry::getInstance();
$registry['index'] = $value;
var_dump( $registry['index'] );

5. Object method access Registry

You will find that using It is also convenient to access registry objects in object style, using the property names in the objects 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. To query whether an index exists

You can use static methods 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 with an ordinary 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 the Registry object

The static registry object is the class Zend_Registry an instance of . 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 the static registry

Although this is not required, you can use the _unsetInstance() method to delete the 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);

I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework. .

For more Zend Framework tutorial Zend_Registry object usage analysis related articles, please pay attention to the PHP Chinese website!

Statement:
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