這篇文章主要介紹了Zend Framework入門教程之Zend_Registry組件用法,結合實例形式分析了Zend_Registry組件實現對象註冊的設置、獲取、判斷、刪除等操作常用技巧,需要的朋友可以參考下
本文實例講述了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>";
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
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的幾個常用的方法與案例。透過註冊表可以實現許多普通變數無法實現的功能。
以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!
相關推薦:
關於Zend Framework處理Json資料的方法
關於Zend Framework中Loader及PluginLoader的用法解析
####################################### #######
以上是關於Zend Framework中Zend_Registry組件的用法分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!