The example in this article describes the usage of Zend_Registry component in Zend Framework. Share it with everyone for your reference, the details are as follows:
1. Object registry
Startup case:
<?php require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Registry'); $member = array( "姓名"=>"张三", "性别"=>"女", "年龄"=>"13", "职业"=>"学生", "爱好"=>"玩游戏", "血型"=>"AB" ); $registry = new Zend_Registry($member); echo "姓名为:"; echo $registry["姓名"]; echo "<p>"; echo "性别为:"; echo $registry["性别"]; echo "<p>"; echo "年龄为:"; echo $registry["年龄"]; echo "<p>"; echo "职业为:"; echo $registry["职业"]; echo "<p>"; echo "爱好为:"; echo $registry["爱好"]; echo "<p>";
Result For:
姓名为:张三 性别为:女 年龄为:13 职业为:学生 爱好为:玩游戏
2.set() method and get() method to set data and get data
Syntax:
Set the value Zend_Registry::set('index','value')
Get the value Zend_Registry::get('index')
Case:
<?php require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Registry'); $member = array( "姓名"=>"张三", "性别"=>"女", "年龄"=>"13", "职业"=>"学生", "爱好"=>"玩游戏", "血型"=>"AB" ); Zend_Registry::set("registry",$member); $registry = Zend_Registry::get("registry"); echo "姓名为:"; echo $registry["姓名"]; echo "<p>"; echo "性别为:"; echo $registry["性别"]; echo "<p>"; echo "年龄为:"; echo $registry["年龄"]; echo "<p>"; echo "职业为:"; echo $registry["职业"]; echo "<p>"; echo "爱好为:"; echo $registry["爱好"]; echo "<p>";
Explanation: The effect is equivalent to the result of new.
3. Object registry setInstance, getInstance
Example:
<?php require_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:
Mike 30 male
Explanation: Through the setInstance method, you can set the value in the form of an object, and then obtain the value through getInstance.
4.isRegistered() to determine whether the index has a value.
Case:
<?php require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Registry'); Zend_Registry::set("name","张三"); if(Zend_Registry::isRegistered("name")){ echo "对象注册表name已经定义"; }else{ echo "对象注册表name没有定义"; } echo "<p>"; if(Zend_Registry::isRegistered("age")){ echo "对象注册表age已经定义"; }else{ echo "对象注册表age没有定义"; }
Result:
对象注册表name已经定义 对象注册表age没有定义
Description:
If defined, it can be detected.
5. Delete the static registry
Zend_Registry::_unsetInstance() method deletes the static registry
Case:
<?php require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Registry'); Zend_Registry::set("name","张三"); echo "执行操作前:"; echo "<p>"; if(Zend_Registry::isRegistered("name")){ echo "对象注册表name已经定义"; }else{ echo "对象注册表name没有定义"; } echo "<p>"; if(Zend_Registry::isRegistered("age")){ echo "对象注册表age已经定义"; }else{ echo "对象注册表age没有定义"; } Zend_Registry::_unsetInstance("name"); echo "<p>"; echo "执行操作后:"; echo "<p>"; if(Zend_Registry::isRegistered("name")){ echo "对象注册表name已经定义"; }else{ echo "对象注册表name没有定义"; } echo "<p>"; if(Zend_Registry::isRegistered("age")){ echo "对象注册表age已经定义"; }else{ echo "对象注册表age没有定义"; }
Result:
执行操作前: 对象注册表name已经定义 对象注册表age没有定义 执行操作后: 对象注册表name没有定义 对象注册表age没有定义
Explanation: After executing the deletion method, the previous registration information will be gone.
Summary:
These are several commonly used methods and cases of Zend_Registry. Many functions that cannot be achieved with ordinary variables can be achieved through the registry.
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.
For more detailed explanations on the usage of the Zend_Registry component in the Zend Framework introductory tutorial, please pay attention to the PHP Chinese website!