当前位置:Gxlcms > PHP教程 > php设计模式入门-注册表模式_PHP教程

php设计模式入门-注册表模式_PHP教程

时间:2021-07-01 10:21:17 帮助过:46人阅读

php设计模式入门-注册表模式


对于这个模式的应用场景不是太好总结,只是根据之前的经验,注册表类里面经常会存储一些别的地方需要用到的对象,比如redis、memcache类,还比如配置信息config类等,它扮演的是一个类似于全局变量的角色。具体的实现其实非常简单,如下代码所示:

containers[$key] = $value;
     }

     public function get($key){
          return isset($this->containers[$key]) ? $this->containers[$key] : null;
     }
}

$registry = Registry::getInstance();
$registry->set('key1', 'hello');	//只是为了测试,通常注册表中存储的数据都是对象
var_dump($registry->get('key1'));
var_dump($registry->get('key2'));

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1040918.htmlTechArticlephp设计模式入门-注册表模式 对于这个模式的应用场景不是太好总结,只是根据之前的经验,注册表类里面经常会存储一些别的地方需要用...

人气教程排行