Skip to content

Instantly share code, notes, and snippets.

@lifesign
Created February 8, 2014 05:19
Show Gist options
  • Save lifesign/8877058 to your computer and use it in GitHub Desktop.
Save lifesign/8877058 to your computer and use it in GitHub Desktop.
simple singleton registry
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(){}
}
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment