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

Usage analysis of Zend_Registry component in Zend Framework

不言
Release: 2023-04-01 10:30:01
Original
1412 people have browsed it

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:

<?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>";
Copy after login

The result is:

姓名为:张三
性别为:女
年龄为:13
职业为:学生
爱好为:玩游戏
Copy after login

##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:

<?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>";
Copy after login

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

3. Object registry setInstance, getInstance

Example:

<?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;
Copy after login

Result:

Mike
30
male
Copy after login

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:

<?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没有定义";
}
Copy after login

Result:

对象注册表name已经定义
对象注册表age没有定义
Copy after login

Description:

If defined, it can be detected.

5. Delete the static registry

Zend_Registry::_unsetInstance() method deletes the static registry

Case:

<?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没有定义";
}
Copy after login

Result:

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

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!

Related labels:
source:php.cn
Statement of this Website
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 admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template