本文实例讲述了Zend Framework中Zend_Registry组件用法。分享给大家供大家参考,具体如下:
1.对象注册表
入门案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?php
require_once ( "Zend/Loader.php" );
Zend_Loader::loadClass( 'Zend_Registry' );
$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>" ;
|
结果为:
1
2
3
4
5
|
姓名为:张三
性别为:女
年龄为:13
职业为:学生
爱好为:玩游戏
|
2.set()方法和get()方法设置数据、获取数据
语法:
设置值Zend_Registry::set('index','value')
获取值Zend_Registry::get('index')
案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?php
require_once ( "Zend/Loader.php" );
Zend_Loader::loadClass( 'Zend_Registry' );
$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>" ;
|
说明:效果等同于new的结果。
3.对象注册表setInstance、getInstance
例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
require_once ( "Zend/Loader.php" );
Zend_Loader::loadClass( 'Zend_Registry' );
$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;
|
结果:
1
2
3
|
Mike
30
male
|
说明:通过setInstance方法,就可以用对象的方式设置其中的值了,然后通过getInstance来获取值。
4.isRegistered()来实现索引是否有值的判断。
案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<?php
require_once ( "Zend/Loader.php" );
Zend_Loader::loadClass( 'Zend_Registry' );
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没有定义" ;
}
|
结果:
1
2
|
对象注册表name已经定义
对象注册表age没有定义
|
说明:
如果定义了,可以检测出来。
5.删除静态注册表
Zend_Registry::_unsetInstance()方法删除静态注册表
案例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?php
require_once ( "Zend/Loader.php" );
Zend_Loader::loadClass( 'Zend_Registry' );
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没有定义" ;
}
|
结果:
1
2
3
4
5
6
|
执行操作前:
对象注册表name已经定义
对象注册表age没有定义
执行操作后:
对象注册表name没有定义
对象注册表age没有定义
|
说明:执行完删除方法之后,之前的注册信息就没有了。
小结:
这就是Zend_Registry的几个常用的方法与案例。通过注册表可以实现很多普通变量无法实现的功能。
希望本文所述对大家基于Zend Framework框架的PHP程序设计有所帮助。