首頁 >php教程 >PHP开发 >Zend Framework入門教學之Zend_Registry組件用法詳解

Zend Framework入門教學之Zend_Registry組件用法詳解

高洛峰
高洛峰原創
2017-01-05 10:28:341339瀏覽

本文實例講述了Zend Framework中Zend_Registry元件用法。分享給大家參考,如下:

1.物件登錄

入門案例:

<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass(&#39;Zend_Registry&#39;);
$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
职业为:学生
爱好为:玩游戏
設定資料、取得資料

語法:

設定值Zend_Registry::set('index','value')

取得值Zend_Registry::get('index')

:效果等同於new的結果。

3.物件註冊表setInstance、getInstance

範例:

<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass(&#39;Zend_Registry&#39;);
$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>";

   

結果:

<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass(&#39;Zend_Registry&#39;);
$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

getInstance來取得值。

4.isRegistered()來實現索引是否有值的判斷。

案例:

<?php
require_once("Zend/Loader.php");
Zend_Loader::loadClass(&#39;Zend_Registry&#39;);
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(&#39;Zend_Registry&#39;);
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没有定义";
}

   

刪除方法之後,之前的註冊資料就沒有了。

小結:

這就是Zend_Registry的幾個常用的方法與案例。透過註冊表可以實現許多普通變數無法實現的功能。

希望本文所述對大家基於Zend Framework框架的PHP程式設計有所幫助。

更多Zend Framework入門教程之Zend_Registry組件用法詳解相關文章請關注PHP中文網!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn