// 注册模式, 解决不用调用直接取来用,将对象注册到全局的树上
class pluginA {
public function __construct()
{
//....
$method = 'some methods...';
// 将 $method 注册到注册树上
Register::set('method', $method);
}
}
class Register {
// Register 树
private static $RegTree;
// reg方法, 将对象$obj, 注册到全局树, 给个$alias 做后续查找用
public static function reg($alias, $obj)
{
self::$RegTree[$alias] = $obj;
}
// get方法, 获取reg已经注册到 Register
public static function get($alias)
{
return self::$RegTree[$alias];
}
// unreg方法, 从注册树找到对象并注销
public static function unreg($alias)
{
unset(self::$RegTree[$alias]);
}
}
//使用的时候直接从 '注册树' 上获取对象, 最大程度降低耦合
RegTree::get('method');
//从注册树上取消
RegTree::unreg('method');