Posted in PHP onDecember 09, 2016
本文实例讲述了Zend Framework中Zend_Registry组件用法。分享给大家供大家参考,具体如下:
1.对象注册表
入门案例:
<?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>";
结果为:
姓名为:张三 性别为:女 年龄为:13 职业为:学生 爱好为:玩游戏
2.set()方法和get()方法设置数据、获取数据
语法:
设置值Zend_Registry::set('index','value')
获取值Zend_Registry::get('index')
案例:
<?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>";
说明:效果等同于new的结果。
3.对象注册表setInstance、getInstance
例子:
<?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;
结果:
Mike 30 male
说明:通过setInstance方法,就可以用对象的方式设置其中的值了,然后通过getInstance来获取值。
4.isRegistered()来实现索引是否有值的判断。
案例:
<?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没有定义"; }
结果:
对象注册表name已经定义 对象注册表age没有定义
说明:
如果定义了,可以检测出来。
5.删除静态注册表
Zend_Registry::_unsetInstance()方法删除静态注册表
案例:
<?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没有定义"; }
结果:
执行操作前: 对象注册表name已经定义 对象注册表age没有定义 执行操作后: 对象注册表name没有定义 对象注册表age没有定义
说明:执行完删除方法之后,之前的注册信息就没有了。
小结:
这就是Zend_Registry的几个常用的方法与案例。通过注册表可以实现很多普通变量无法实现的功能。
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。
Zend Framework入门教程之Zend_Registry组件用法详解
- Author -
牛逼的霍啸林声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@