Home  >  Article  >  Backend Development  >  Usage analysis of Zend_Registry component in Zend Framework

Usage analysis of Zend_Registry component in Zend Framework

不言
不言Original
2018-06-15 14:33:111231browse

This article mainly introduces the usage of the Zend_Registry component in the Zend Framework introductory tutorial, and analyzes the common techniques of Zend_Registry component to implement object registration settings, acquisition, judgment, deletion and other operations in the form of examples. Friends in need can refer to it

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

Getting Started Case:

"张三",
  "性别"=>"女",
  "年龄"=>"13",
  "职业"=>"学生",
  "爱好"=>"玩游戏",
  "血型"=>"AB"
  );
$registry = new Zend_Registry($member);
echo "姓名为:";
echo $registry["姓名"];
echo "

"; echo "性别为:"; echo $registry["性别"]; echo "

"; echo "年龄为:"; echo $registry["年龄"]; echo "

"; echo "职业为:"; echo $registry["职业"]; echo "

"; echo "爱好为:"; echo $registry["爱好"]; echo "

";

The result is:

姓名为:张三
性别为:女
年龄为: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:

"张三",
  "性别"=>"女",
  "年龄"=>"13",
  "职业"=>"学生",
  "爱好"=>"玩游戏",
  "血型"=>"AB"
  );
Zend_Registry::set("registry",$member);
$registry = Zend_Registry::get("registry");
echo "姓名为:";
echo $registry["姓名"];
echo "

"; echo "性别为:"; echo $registry["性别"]; echo "

"; echo "年龄为:"; echo $registry["年龄"]; echo "

"; echo "职业为:"; echo $registry["职业"]; echo "

"; echo "爱好为:"; echo $registry["爱好"]; echo "

";

Description: The effect is equivalent to the result of new.

3. Object registry setInstance, getInstance

Example:

name = "Mike";
$registry->age = "30";
$registry = Zend_Registry::getInstance();
echo $registry->name;
echo "

"; echo $registry->age; echo "

"; $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:

";
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:

";
if(Zend_Registry::isRegistered("name")){
  echo "对象注册表name已经定义";
}else{
  echo "对象注册表name没有定义";
}
echo "

"; if(Zend_Registry::isRegistered("age")){ echo "对象注册表age已经定义"; }else{ echo "对象注册表age没有定义"; } Zend_Registry::_unsetInstance("name"); echo "

"; echo "执行操作后:"; echo "

"; if(Zend_Registry::isRegistered("name")){ echo "对象注册表name已经定义"; }else{ echo "对象注册表name没有定义"; } echo "

"; if(Zend_Registry::isRegistered("age")){ echo "对象注册表age已经定义"; }else{ echo "对象注册表age没有定义"; }

Result:

执行操作前:
对象注册表name已经定义
对象注册表age没有定义
执行操作后:
对象注册表name没有定义
对象注册表age没有定义

Description: 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.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About Zend Framework’s method of processing Json data

About the usage of Loader and PluginLoader in Zend Framework Analysis

About the usage of Zend Framework action controller

##

The above is the detailed content of Usage analysis of Zend_Registry component in Zend Framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]