a tiny mvc framework for php using php-activerecord
1<?php
2/*
3 singleton, taken from php-activerecord
4
5 Copyright (c) 2009
6
7 AUTHORS:
8 Kien La
9 Jacques Fuentes
10
11 Permission is hereby granted, free of charge, to any person obtaining a copy
12 of this software and associated documentation files (the "Software"), to deal
13 in the Software without restriction, including without limitation the rights
14 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 copies of the Software, and to permit persons to whom the Software is
16 furnished to do so, subject to the following conditions:
17
18 The above copyright notice and this permission notice shall be included in
19 all copies or substantial portions of the Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 THE SOFTWARE.
28*/
29
30namespace HalfMoon;
31
32/**
33 * This implementation of the singleton pattern does not conform to the strong definition
34 * given by the "Gang of Four." The __construct() method has not be privatized so that
35 * a singleton pattern is capable of being achieved; however, multiple instantiations are also
36 * possible. This allows the user more freedom with this pattern.
37 */
38abstract class Singleton
39{
40 /**
41 * Array of cached singleton objects
42 * @static
43 * @var array
44 */
45 private static $instances = array();
46
47 /**
48 * Static method for instantiating a singleton object.
49 * @static
50 * @return object
51 */
52 final public static function instance()
53 {
54 $class_name = get_called_class();
55
56 if (!isset(self::$instances[$class_name]))
57 self::$instances[$class_name] = new $class_name;
58
59 return self::$instances[$class_name];
60 }
61
62 /**
63 * Singleton objects should not be cloned
64 * @return void
65 */
66 final private function __clone() {}
67
68 /**
69 * Similar to a get_called_class() for a child class to invoke.
70 * @return string
71 */
72 final protected function get_called_class()
73 {
74 $backtrace = debug_backtrace();
75 return get_class($backtrace[2]['object']);
76 }
77}
78
79?>