Home > php教程 > php手册 > zf框架的registry(注册表)使用示例

zf框架的registry(注册表)使用示例

WBOY
Release: 2016-06-13 09:40:52
Original
1080 people have browsed it

简单使用

复制代码 代码如下:


require_once("Zend/Loader.php");
Zend_Loader::loadClass("Zend_Registry");
$Arr = array
(
 'host' => '127.0.0.1',
 'username' => 'root',
 'password' => '111',
 'dbname' => 'test'
);
$Reg = new Zend_Registry($Arr);
echo '主机名:' . $Reg['host'] . "
";
echo '用户名:' . $Reg['username'] . "
";
echo '密码:' . $Reg['password'] . "
";
echo '数据库:' . $Reg['dbname'] . "
";
echo "


";
Zend_Registry::set('表名','sanguo');  //SET赋值方法,也可以赋值为数组
echo Zend_Registry::get('表名');  //GET取值方法
?>

对象方式使用与set、get方法使用

复制代码 代码如下:


//引入Loader自动载入类
require_once("Zend/Loader.php");
//载入注册表对象类
Zend_Loader::loadClass("Zend_Registry");
/*--------------------------------------------------------*/
//以对象方式进行注册表操作
//实例化注册表对象类的资源赋给$Reg
$Reg = new Zend_Registry();
//讲$Reg转换为对象格式
Zend_Registry::setInstance($Reg);
//对$Reg进行赋值(注册表赋值)
$Reg ->name = '张三';
$Reg ->sex  = '男';
$Reg ->age  = '18';
//获取静态对象后输出.
$Reg = Zend_Registry::getInstance();
echo "姓名为:" . $Reg->name . "
";
echo "性别为:" . $Reg->sex . "
";
echo "年龄为:" . $Reg->age . "
";
/*--------------------------------------------------------*/
$Arr = array('姓名' => '张三','年龄' => '18','爱好' => '上网');
Zend_Registry::set('My',$Arr);
class Person
{
 public function My()
 {
  echo "我的姓名是:" . Zend_Registry::get('My')['姓名'] . "
";
  echo "我的年龄是:" . Zend_Registry::get('My')['年龄'] . "
";
  echo "我的爱好是:" . Zend_Registry::get('My')['爱好'] . "
";
 }
}
$Person = new Person();
$Person -> My();
?>

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template