本文实例讲述了Zend Framework中Zend_Registry组件用法。分享给大家供大家参考,具体如下:
1.对象注册表
入门案例:
<?phprequire_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')
案例:
<?phprequire_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
例子:
<?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;
结果:
Mike30male
说明:通过setInstance方法,就可以用对象的方式设置其中的值了,然后通过getInstance来获取值。
4.isRegistered()来实现索引是否有值的判断。
案例:
<?phprequire_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没有定义";}
新闻热点
疑难解答