forked from zanbaldwin/rsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.php
68 lines (62 loc) · 1.57 KB
/
library.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
<?php
/**
* Library Library
*
* The Library library is designed to only allow one instance of any library that extends it to exist per application or
* module. Adapted from the Eventing project <https://github.com/lesshub/eventing>.
*
* @category Eventing
* @package Libraries
* @subpackage Library
*/
/**
* Library Base Class
*
* Implementation fo the Singleton design pattern. Instances of classes can be returned with the getInstance()
* method, whilst new instances are forbidden.
*/
abstract class library {
/**
* Constructor Function
*
* Every library class must have a constructor function that is defined
* using the protected access availability.
*
* @abstract
* @access protected
*/
abstract protected function __construct();
/**
* Disallow Cloning
*
* No point using the singleton pattern if the object can be cloned into a new instance.
*
* @access public
* @return E_USER_ERROR
*/
final public function __clone() {
trigger_error('Cannot clone singleton library.', E_USER_ERROR);
}
/**
* Get Instance
*
* @final
* @static
* @access public
* @return object|false
*/
final public static function &getInstance() {
static $objects = array();
$class = get_called_class();
if(isset($objects[$class]) && is_object($objects[$class])) {
return $objects[$class];
}
if(!class_exists($class)) {
return false;
}
$objects[$class] = isset($class::$_instance) && is_object($class::$_instance)
? $class::$_instance
: new $class;
return $objects[$class];
}
}