| | 5 | |
| | 6 | /// Singleton implementation. |
| | 7 | abstract class Singleton { |
| | 8 | private static $instances = array(); |
| | 9 | |
| | 10 | protected function __construct() { |
| | 11 | } |
| | 12 | |
| | 13 | final protected static function _getInstance($className) { |
| | 14 | if (!array_key_exists($className, self::$instances)) { |
| | 15 | self::$instances[$className] = new $className(); |
| | 16 | } |
| | 17 | return self::$instances[$className]; |
| | 18 | } |
| | 19 | |
| | 20 | /* |
| | 21 | // You should implement this method to the final class. (An example is below.) |
| | 22 | // This is mainly because "late static bindings" is supported after PHP 5.3. |
| | 23 | |
| | 24 | public static function getInstance() { |
| | 25 | return self::_getInstance(__CLASS__); |
| | 26 | } |
| | 27 | */ |
| | 28 | abstract public static function getInstance(); |
| | 29 | } |
| | 30 | |