Created
February 8, 2014 05:19
-
-
Save lifesign/8877058 to your computer and use it in GitHub Desktop.
simple singleton registry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(){} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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