registry-classe
Code (php)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
class Registry implements ArrayAccess, Iterator, Countable {
private $globals;
// Magic methods
public function __construct() {
$this->globals = array();
}
public function __destruct() {
}
public function __get( $key ) {
return $this->offsetGet ( $key );
}
public function __set ( $key, $value ) {
$this->offsetSet ( $key, $value );
}
public static function getInstance() {
static $instance;
if (! is_object ( $instance ) ) {
$obj = __CLASS__;
$instance = new $obj;
}
return $instance;
}
// CLASS METHODS
// Voegt een nieuw element toe aan de $this->globals array
public function add ( $global, $key = '' ) {
if ( strlen ($key) > 0 ) {
if ( ! $this->offsetExists ( $key ) ) {
$this->globals [ $key ] = &$global;
} else {
throw new Exception("Duplicate etry found");
}
} else {
$this->globals[] = &$global;
}
}
// Verwijder een element uit de globals array
public function delete ( $key ) {
$this->offsetUnset ( $key );
}
// Verwijder alle elementen uit de globals array
public function clear () {
$this->globals = array();
}
// Geef het een bepaald element terug
public function get ( $offset ) {
return $this->offsetGet ( $offset );
}
// IMPLEMENTATIONS
//-- ArrayAccess
public function offsetExists ( $offset ) {
return isset ( $this->globals [ $offset ] );
}
public function offsetGet ( $offset ) {
if ( $this->offsetExists ( $offset ) ) {
return $this->globals [ $offset ];
} else {
throw new RangeException("Index out of bounds");
}
}
public function offsetSet ( $offset, $value ) {
if ( $this->offsetExists ( $offset ) ) {
$this->globals [ $offset ] = &$value;
} else {
throw new RangeException("Index out of bounds");
}
}
public function offsetUnset ( $offset ) {
if ( $this->offsetExists ( $offset ) ) {
unset ( $this->globals [ $offset ] );
} else {
throw new RangeException("Index out of bounds");
}
}
//-- Iterator
public function current() {
return current ( $this->globals );
}
public function next() {
return next ( $this->globals );
}
public function key() {
return key ( $this->globals );
}
public function rewind() {
reset ( $this->globals );
}
public function valid() {
return $this->current() !== false;
}
//-- Countable
public function count () {
return count ( $this->globals );
}
}
?>
class Registry implements ArrayAccess, Iterator, Countable {
private $globals;
// Magic methods
public function __construct() {
$this->globals = array();
}
public function __destruct() {
}
public function __get( $key ) {
return $this->offsetGet ( $key );
}
public function __set ( $key, $value ) {
$this->offsetSet ( $key, $value );
}
public static function getInstance() {
static $instance;
if (! is_object ( $instance ) ) {
$obj = __CLASS__;
$instance = new $obj;
}
return $instance;
}
// CLASS METHODS
// Voegt een nieuw element toe aan de $this->globals array
public function add ( $global, $key = '' ) {
if ( strlen ($key) > 0 ) {
if ( ! $this->offsetExists ( $key ) ) {
$this->globals [ $key ] = &$global;
} else {
throw new Exception("Duplicate etry found");
}
} else {
$this->globals[] = &$global;
}
}
// Verwijder een element uit de globals array
public function delete ( $key ) {
$this->offsetUnset ( $key );
}
// Verwijder alle elementen uit de globals array
public function clear () {
$this->globals = array();
}
// Geef het een bepaald element terug
public function get ( $offset ) {
return $this->offsetGet ( $offset );
}
// IMPLEMENTATIONS
//-- ArrayAccess
public function offsetExists ( $offset ) {
return isset ( $this->globals [ $offset ] );
}
public function offsetGet ( $offset ) {
if ( $this->offsetExists ( $offset ) ) {
return $this->globals [ $offset ];
} else {
throw new RangeException("Index out of bounds");
}
}
public function offsetSet ( $offset, $value ) {
if ( $this->offsetExists ( $offset ) ) {
$this->globals [ $offset ] = &$value;
} else {
throw new RangeException("Index out of bounds");
}
}
public function offsetUnset ( $offset ) {
if ( $this->offsetExists ( $offset ) ) {
unset ( $this->globals [ $offset ] );
} else {
throw new RangeException("Index out of bounds");
}
}
//-- Iterator
public function current() {
return current ( $this->globals );
}
public function next() {
return next ( $this->globals );
}
public function key() {
return key ( $this->globals );
}
public function rewind() {
reset ( $this->globals );
}
public function valid() {
return $this->current() !== false;
}
//-- Countable
public function count () {
return count ( $this->globals );
}
}
?>
Wat voorbeelden van hoe te gebruiken:
Code (php)
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
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
<?php
$reg = Registry::getInstance();
$reg->add ( $db, 'database' );
$reg->add ( $user, 'user' );
$reg->add ( $template, 'template' );
echo $reg->database;
echo $reg->user;
echo $reg['template'];
foreach ( $reg as $t ) {
echo $t;
}
class blaat {
private $db;
public function __construct() {
$this->db = Registry::getInstance()->database;
$this->user = Registry::getInstance()->get ('user');
}
public function blabla () {
$reg = Registry::getInstance();
$reg['db']->query();
}
}
?>
$reg = Registry::getInstance();
$reg->add ( $db, 'database' );
$reg->add ( $user, 'user' );
$reg->add ( $template, 'template' );
echo $reg->database;
echo $reg->user;
echo $reg['template'];
foreach ( $reg as $t ) {
echo $t;
}
class blaat {
private $db;
public function __construct() {
$this->db = Registry::getInstance()->database;
$this->user = Registry::getInstance()->get ('user');
}
public function blabla () {
$reg = Registry::getInstance();
$reg['db']->query();
}
}
?>