_php instance of Zend_registry object usage Analysis in Zend framework tutorial

Source: Internet
Author: User
Tags scalar zend zend framework

This example describes the Zend_registry object usage of the Zend Framework tutorial. Share to everyone for your reference, specific as follows:

Using the Object Registry (Registry)

The object registry (or Object warehouse) is a container for storing objects and values within the entire application space (application spaces). By storing objects in them, we can use the same object anywhere in the entire project. This mechanism is equivalent to a global storage.

We can use the object registry using the static method of the Zend_registry class, and because the class is an array object, you can use the array form to access the class method.

1. Set the value in registry

To save a piece of content into the registry, we can use the static method set ().

Example 1. Set () Use example:

Zend_registry::set (' index ', $value);

$value can be an object, an array, or a scalar. You can use Set () again to set a new value for the values already in the registry.

The index argument can be a scalar, a string or an integer, just like an array, an index/key name similar to an array.

2. Get the value in registry

You can use the Get () method to obtain the value of an item in the registry.

Example 2. Get () Method Example:

$value = Zend_registry::get (' index ');

GetInstance () Returns a static registry object.
The registry object is an iterative (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 a static method to access a registry object, you can instantiate it directly, just as you would with a normal object.

If you use a static method to access an instance of a registry object, 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 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 you create this object instance, you can use the Array object method to use it, or you can set the object instance through a static method Setinstance () to a static object instance.

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 already been initialized, the Setinstance () method throws a Zend_exception exception.

4. Access to registry objects like array access

If you want to access or set multiple values at once, you will find it convenient to use an array method.

Example 6. Array Mode Access Example:

$registry = Zend_registry::getinstance ();
$registry [' index '] = $value;
Var_dump ($registry [' Index ']);

5. Object Mode Access Registry

You will find it convenient to use object-oriented style to access registry objects, which are indexed as property names in objects. To do this, you need to use the ARRAYOBJECT::ARRAY_AS_PROPS option to create the registry object and initialize the static instance. You have to finish the job before the static registry is first accessed. Use this option carefully, because some versions of PHP will have bugs when using this option.

Example 7. Access to Object form:

In your bootstrap code:
$registry = new Zend_registry (Array (), arrayobject::array_as_props)
zend_registry:: Setinstance ($registry);
$registry->tree = ' Apple ';
In any other place of the program:
$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 particular index has set the appropriate value.

Example 8. Isregistered () Example:

if (zend_registry::isregistered ($index)) {
  $value = Zend_registry::get ($index);
}

To determine whether the value of a particular index in an array object is set, you can use the Isset () function as you would 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. Extended Registry Objects

A static registry object is an instance of a 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 use of this class. Note that this class must be a zend_registry subclass.

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, registry throws an exception. It is recommended that you set the class name in Boostrap code (that is, index.php).

8. Delete static registry

Although this is not required, you can use the _unsetinstance () method to remove static instances of registry.

[note] The risk of data loss

When _unsetinstance () is used, all data in the static registry is lost and cannot be recovered.

Sometimes you may need to _unsetinstance () this method. For example, if you want to use Setinstance () or Setclassname () after the registry object has already been initialized, then you can use _unsetinstance () to remove the static instance before using those methods.

Example 11. _unsetinstance () Example:

Zend_registry::set (' index ', $value);
Zend_registry::_unsetinstance ();
Change the class
zend_registry::setclassname (' my_registry ') that we want to use;
Zend_registry::set (' index ', $value);

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.