Zend Framework Tutorial Zend_registry Object Usage analysis, zendzend_registry_php tutorial

Source: Internet
Author: User
Tags scalar zend framework

Zend Framework Tutorial Zend_registry Object Usage analysis, Zendzend_registry


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

Using the Object Registry (Registry)

The object registry (or object repository) 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 store.

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

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 value already in the registry.

The index parameter can be a scalar, that is, a string or an integer, just like an array, like an index/key name for an array.

2. Get the value in registry

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

Example 2. Example of the Get () method:

$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 Cont ains:/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 as you would with a normal object.

If you access an instance of a 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 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 the object instance, you can use it using the Array object method, or you can set the 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 a static registry object has already been initialized, the Setinstance () method throws a Zend_exception exception.

4. Access the Registry object as you would access an array

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 styles to access the registry object, which is indexed by the property name in the object. 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 do this work before the static registry is first accessed. Be careful with this option, because some versions of PHP will have bugs when using this option.

Example 7. Object-type access:

In your bootstrap code: $registry = new Zend_registry (Array (), Arrayobject::array_as_props) zend_registry::setinstance ($ registry); $registry->tree = ' apple ', ...//anywhere else in 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 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 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 syntaxif (isset ($registry [' Index '])) {  Var_dump ($ registry[' index ');} Using object-access syntax, if ENABLEDIF (Isset ($registry->index)) {  var_dump ($registry->index);}

7. Extending the Registry Object

A static registry object is an instance of the class Zend_registry. If you want to add functionality to it, you can inherit the Zend_registry class and then specify that the class be used 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 subclass of Zend_registry.

Example 10. Specify the class name for the static registry:

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

If you try to set the class name after registry has been accessed, registry throws an exception. It is recommended that you set the class name in the Boostrap code (that is, index.php).

8. Delete the static registry

Although this is not required, you can use the _unsetinstance () method to delete a static instance of registry.

[note] The risk of data loss

When using _unsetinstance (), 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 you have initialized the registry object, 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 ();//Change the Class Zend_registry::setclassname (' My_registry ') we want to use; Zend_registry::set (' index ', $value);

More interested in Zend related content readers can view the topic: "Zend framework of the introductory tutorial", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Tutorial", "PHP object-oriented Programming introduction tutorial "," Introduction to Php+mysql Database Operation "and" PHP common database Operation Skills Summary "

It is hoped that this article will help you to design PHP based on the Zend Framework framework.

Articles you may be interested in:

    • Zend output generates XML parsing error
    • Application analysis of config mechanism based on Zend
    • Zend Framework implementation of multi-server sharing session data method
    • Zend Framework Smarty Extension Implementation method
    • Code analysis of routing mechanism of Zend framework framework
    • Zend Framework Implementation Message Sub-page function (with demo source download)
    • The Zend framework implements the method of storing the session in Memcache
    • Zend Framework Paging Class usage
    • Zend framework generate verification code and implement Verification Code verification function (with demo source download)
    • Zend Framework implements multi-file upload function instances
    • Zend Framework Introduction Environment configuration and the first Hello World example (with demo source download)
    • Zend Framework Introductory Knowledge Point Summary
    • Zend_config_xml usage Analysis of Zend Framework Tutorial

http://www.bkjia.com/PHPjc/1113716.html www.bkjia.com true http://www.bkjia.com/PHPjc/1113716.html techarticle Zend Framework Tutorial Zend_registry Object Usage Analysis, Zendzend_registry This example describes the Zend_registry object usage of the Zend Framework tutorial. Share to everyone for reference, with ...

  • Related Article

    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.