Home > Backend Development > PHP Tutorial > Getting Started with PHP Design Patterns - Registry Pattern_PHP Tutorial

Getting Started with PHP Design Patterns - Registry Pattern_PHP Tutorial

WBOY
Release: 2016-07-13 09:45:28
Original
801 people have browsed it

Introduction to PHP Design Pattern - Registry Pattern

It’s not easy to summarize the application scenarios of this model. But based on previous experience, registry classes often store some objects that need to be used elsewhere, such as redis, memcache classes, and configuration information config classes, etc. , which plays a role similar to a global variable. The specific implementation is actually very simple, as shown in the following code:

<!--?php
class Registry{
     static $instance;
     public $containers = array();

     static function getInstance(){
          if(is_null(self::$instance)){
               self::$instance = new self();
          }
          return self::$instance;
     }

     public function set($key, $value){
          $this--->containers[$key] = $value;
     }

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

$registry = Registry::getInstance();
$registry->set(&#39;key1&#39;, &#39;hello&#39;);<span style="white-space:pre">	</span>//只是为了测试,通常注册表中存储的数据都是对象
var_dump($registry->get(&#39;key1&#39;));
var_dump($registry->get(&#39;key2&#39;));
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1040918.htmlTechArticleIntroduction to PHP Design Pattern - Registry Pattern is not a good summary of the application scenarios of this pattern, just based on previous experience , the registry class often stores some other places that need to be used...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template