PHP 注册树模式
/** * 注册树模式 * 将对象注册到一个类中 * 通过该类实现全局访问操作对象 */ class Tree { private static $treeList = []; private function __construct(){} private function __clone(){} public static function set($key,$value) { //设置节 self::$treeList[$key] = $value; } public static function get($key) { //获取节 return self::$treeList[$key]; } public static function delete($key) { //删除节 unset(self::$treeList[$key]); } } class Mysql { } class Slave { } //注册对象 Tree::set('write',new Mysql()); Tree::set('read',new Slave()); //获取对象 Tree::get('read'); //删除对象 Tree::delete('write');