This repository was archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.php
85 lines (72 loc) · 1.45 KB
/
registry.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
<?php
/**
* Contains MVC_Registry class
*
*/
namespace SmallPHPMVC;
/**
* A singleton registry class which can hold application-wide settings.
*
* Example:
* <code>
* $registry = MVC_Registry::getReg();
* $db = <db setup>;
* $registry->set('db', $db);
* OR
* $registry['db'] = $db;
*
*
* // Somewhere else in the application...
* $registry = MVC_Registry::getReg();
* $db = $registry->get('db');
* OR
* $db = $registry['db'];
* </code>
*
* @author David Lancea
*/
class Registry implements \ArrayAccess {
private $vars = array();
private static $instance = null;
/**
* Makes this class a singleton
*
* @return type
*/
static function getReg(){
if(self::$instance == null){
self::$instance = new Registry();
return self::$instance;
}else{
return self::$instance;
}
}
function set($key, $var) {
if (isset($this->vars[$key]) == true) {
throw new \Exception('Unable to set var `' . $key . '`. Already set.');
}
$this->vars[$key] = $var;
return true;
}
function get($key) {
if (isset($this->vars[$key]) == false) {
return null;
}
return $this->vars[$key];
}
function remove($key) {
unset($this->vars[$key]);
}
function offsetExists($offset) {
return isset($this->vars[$offset]);
}
function offsetGet($offset) {
return $this->get($offset);
}
function offsetSet($offset, $value) {
$this->set($offset, $value);
}
function offsetUnset($offset) {
unset($this->vars[$offset]);
}
}