注册表模式可似把他想像成一个全局变量,所有的模块都从这个全局变量里存取数据,或者也可以想象成某个酒吧的许愿墙或留言版,上面的内容大家都可以看到,也可以改写。这里主要按作用域介绍三种类别的注册表类(请求级别、会话级别、应用程序级别)。
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
namespace woo\base;
//基类
abstract class Registry {
abstract protected function get( $key );
abstract protected function set( $key , $val );
}
//请求级别,他的生存周期通常为从用户发起一个请求到后台程序回复这个请求为止
class RequestRegistry extends Registry{
private $values = array ();
private static $instance ;
private function __construct (){}
static function instance(){ // 单例,即这个类只有一个唯一的实例
if (!isset(self:: $instance )){
self:: $instance = new self();
}
return self:: $instance ;
}
protected function get( $key ){
if (isset( $this ->values[ $key ]){
return $this ->values[ $key ];
}
return null;
}
protected function set( $key , $val ){
$this ->values[ $key ] = $val ;
}
static function getRequest(){
return self::instance()->get( 'request' );
}
static function setRequest(\woo\controller\Request $request ){ //\woo\controller\Request 主要功能是处理用户请求信息的一个类
return self::instance()->set( 'request' , $request );
}
}
//会话级别,此示例中类的生存周期主要还是看SESSION的生存时间
class SessionRegistry extends Registry{
private static $instance ;
private function __construct (){
session_start();
}
static function instance(){
if (!isset(self:: $instance )){
self:: $instance = new self();
}
return self:: $instance ;
}
protected function get( $key ){
if (isset( $_SESSION [ __CLASS__ ][ $key ])){
return $_SESSION [ __CLASS__ ][ $key ];
}
return null;
}
protected function set( $key , $val ){
$_SESSION [ __CLASS__ ][ $key ] = $val ;
}
function setComplex(Complex $complex ){
self::instance()->set( 'complex' , $complex );
}
function getComplex(){
return self::instance()->get( 'complex' );
}
}
//应用程序级别,此示例中因相关的值是保存在文本文件中,所以只要文件存在,保存的值也就一直存在
class ApplicationRegistry extends Registry{
private Static $instance ;
private $freezedir = 'data' ;
private $values = array ();
private $mtimes = array ();
private function __construct (){}
static function instance(){
if (!isset(self:: $instance )){
self:: $instance = new self();
}
return self:: $instance ;
}
protected function get( $key ){
$path = $this ->freezedir . DIRECTORY_SEPARATOR . $key ; // 保存值的文件的路径
if ( file_exists ( $path )){
clearstatcache(); // 清除filemtime缓存的上次记录的文件修改时间
$mtime = filemtime ( $path );
if (!isset( $this ->mtimes[ $key ])){
$this ->mtimes[ $key ] = 0;
}
if ( $mtime > $this ->mtimes[ $key ]){ // 文件内容假如被修改过,那么就要重新获取里面的值
$data = file_get_contents ( $path );
$this ->mtimes[ $key ] = $mtime ;
return ( $this ->values[ $key ] = unserialize( $data ));
}
}
if (isset( $this ->values[ $key ])){
return $this ->values[ $key ];
}
return null;
}
protected function set ( $key , $val ){
$this ->values[ $key ] = $val ;
$path = $this ->freezedir . DIRECTORY_SEPARATOR . $key ;
file_put_contents ( $path ,serialize( $val ));
$this ->mtimes[ $key ] = time();
}
static function getDSN(){
return self::instance()->get( 'dsn' );
}
static function setDSN( $dsn ){
return self::instance()->set( 'dsn' , $dsn );
}
}
|
以上这篇老生常谈PHP面向对象之注册表模式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。