Maison >développement back-end >tutoriel php >zf框架的registry(注册表)使用示例_PHP
简单使用
代码如下:
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 "
对象方式使用与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();
?>