lifesign
2/8/2014 - 5:19 AM

simple singleton registry

simple singleton registry

abstract class singleton {
    static function instance() {
        if (!Registry::exists($class = get_called_class())) {
            $ref = new Reflectionclass($class);
            $args = func_get_args();
            Registry::set($class, $args ? $ref->newinstanceargs($args) : new $class);
        }
        
        return Registry::get($key);
    }
}
class Registry {
    private static $table;
  
    static function exists($key) {
        return isset(self::$table[$key]);
    }
    
    static function set($key, $val) {
        return self::$table[$key] = $val;
    }
    
    static function get($key, $default = null) {
        return static::exists($key) ? self::$table[$key] : $default;
    }
    
    static function clear($key) {
        self::$table[$key] = null;
        unset(self::$table[$key]);
    }
    
    private function __clone(){}
    
    private function __construct(){}

}