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
|
<?php /** * @package ActiveRecord */ namespace ActiveRecord;
/** * This implementation of the singleton pattern does not conform to the strong definition * given by the "Gang of Four." The __construct() method has not be privatized so that * a singleton pattern is capable of being achieved; however, multiple instantiations are also * possible. This allows the user more freedom with this pattern. * * @package ActiveRecord */ abstract class Singleton { /** * Array of cached singleton objects. * * @var array */ private static $instances = array();
/** * Static method for instantiating a singleton object. * * @return object */ final public static function instance() { $class_name = get_called_class();
if (!isset(self::$instances[$class_name])) self::$instances[$class_name] = new $class_name;
return self::$instances[$class_name]; }
/** * Singleton objects should not be cloned. * * @return void */ final private function __clone() {}
/** * Similar to a get_called_class() for a child class to invoke. * * @return string */ final protected function get_called_class() { $backtrace = debug_backtrace(); return get_class($backtrace[2]['object']); } } ?>
|