让我们想象一下我们有注册表模式......
<?php
class Registry
{
private static $objects = array();
private static $instance = null;
public static function getInstance() {
if (self::$instance == null) {
self::$instance = new Registry();
}
return self::$instance;
}
protected function _get($key) {
return ($this->objects[$key]) ? $this->objects[$key] : null;
}
protected function _set($key, $val) {
$this->objects[$key] = $val;
}
public static function get($key) {
return self::getInstance()->_get($key);
}
public static function set($key, $object) {
return self::getInstance()->_set($key, $object);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
使用这个实现非常简单......
<?
Registry::set('db', $db_client);
Registry::set('redis', $redis_client);
//Using …Run Code Online (Sandbox Code Playgroud)