Zend_Registry component usage in Zend Framework getting started tutorial, zendzend_registry

Source: Internet
Author: User

Zend_Registry component usage in Zend Framework getting started tutorial, zendzend_registry

This article describes the Zend_Registry component usage in Zend Framework. We will share this with you for your reference. The details are as follows:

1. Object Registry

Entry case:

<? Phprequire_once ("Zend/Loader. php "); Zend_Loader: loadClass ('zend _ Registry '); $ member = array (" name "=>" Zhang San "," gender "=>" female ", "Age" => "13", "Occupation" => "Students", "hobbies" => "Play Games", "blood type" => "AB "); $ registry = new Zend_Registry ($ member); echo "name:"; echo $ registry ["name"]; echo "<p>"; echo "Gender :"; echo $ registry ["gender"]; echo "<p>"; echo "Age:"; echo $ registry ["Age"]; echo "<p> "; echo "Occupation:"; echo $ registry ["Occupation"]; echo "<p>"; echo "Hobbies:"; echo $ registry ["hobby"]; echo "<p> ";

Result:

Name: Zhang San Gender: Female Age: 13 occupation: Students hobby: Play Games

2. set () and get () Methods to set and obtain data

Syntax:

Set Zend_Registry: set ('index', 'value ')
Get Zend_Registry: get ('index ')

Case:

<? Phprequire_once ("Zend/Loader. php "); Zend_Loader: loadClass ('zend _ Registry '); $ member = array (" name "=>" Zhang San "," gender "=>" female ", "Age" => "13", "Occupation" => "Students", "hobbies" => "Play Games", "blood type" => "AB"); Zend_Registry :: set ("registry", $ member); $ registry = Zend_Registry: get ("registry"); echo "name:"; echo $ registry ["name"]; echo "<p>"; echo "Gender:"; echo $ registry ["gender"]; echo "<p>"; echo "Age :"; echo $ registry ["Age"]; echo "<p>"; echo "Occupation:"; echo $ registry ["Occupation"]; echo "<p> "; echo "Hobbies:"; echo $ registry ["hobbies"]; echo "<p> ";

Note: The effect is equivalent to the result of new.

3. Object registry setInstance and getInstance

Example:

<?phprequire_once("Zend/Loader.php");Zend_Loader::loadClass('Zend_Registry');$registry = new Zend_Registry();Zend_Registry::setInstance($registry);$registry->name = "Mike";$registry->age = "30";$registry = Zend_Registry::getInstance();echo $registry->name;echo "<p>";echo $registry->age;echo "<p>";$registry->sex = "male";echo $registry->sex;

Result:

Mike30male

Note: Through the setInstance method, you can set the value in the object method, and then get the value through getInstance.

4. isRegistered () is used to determine whether the index has a value.

Case:

<? Phprequire_once ("Zend/Loader. php "); Zend_Loader: loadClass ('zend _ Registry '); Zend_Registry: set (" name "," zhangsan "); if (Zend_Registry :: isRegistered ("name") {echo "the object registry name has been defined";} else {echo "the object registry name is not defined";} echo "<p> "; if (Zend_Registry: isRegistered ("age") {echo "the object registry age has been defined";} else {echo "the object registry age is not defined ";}

Result:

Object registry name Defined Object registry age not defined

Note:

If it is defined, it can be detected.

5. Delete the static Registry

Zend_Registry: _ unsetInstance () method to delete a static Registry

Case:

<? Phprequire_once ("Zend/Loader. php "); Zend_Loader: loadClass ('zend _ Registry '); Zend_Registry: set (" name "," Zhang San "); echo" before executing the operation :"; echo "<p>"; if (Zend_Registry: isRegistered ("name") {echo "the object registry name has been defined ";} else {echo "the object registry name is not defined";} echo "<p>"; if (Zend_Registry: isRegistered ("age ")) {echo "the object registry age has been defined";} else {echo "the object registry age is not defined";} Zend_Registry: _ unsetInstance ("name "); echo "<p>"; echo "after the operation is executed:"; echo "<p>"; if (Zend_Registry: isRegistered ("name ")) {echo "the object registry name has been defined";} else {echo "the object registry name is not defined";} echo "<p>"; if (Zend_Registry :: isRegistered ("age") {echo "the object registry age has been defined";} else {echo "the object registry age is not defined ";}

Result:

Before performing the operation: After the object registry name has been defined and the object registry age has not been defined, the object registry name is not defined, and the object registry age is not defined.

Note: After the deletion method is executed, the previous registration information is gone.

Summary:

This is a few common Zend_Registry methods and cases. You can use the registry to implement many functions that cannot be implemented by common variables.

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.