Home  >  Article  >  Backend Development  >  PHP design pattern registry pattern (registration of multiple classes)_PHP tutorial

PHP design pattern registry pattern (registration of multiple classes)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:20:471131browse

I have also written a registry class before, but that one cannot register multiple classes. The classes are stored in an array below.

Copy code The code is as follows:

//Basic class
class webSite {/ /A very simple base class
private $siteName;
private $siteUrl;
function __construct($siteName,$siteUrl){
$this->siteName=$siteName;
$this->siteUrl=$siteUrl;
}
function getName(){
return $this->siteName;
}
function getUrl(){
return $ this->siteUrl;
}
}
class registry {//Registry class singleton mode
private static $instance;
private $values=array();//Use Array stores class name
private function __construct(){}//This usage determines that this class cannot be instantiated directly
static function instance(){
if (!isset(self::$instance)) {self::$instance=new self();}
return self::$instance;
}
function get($key){//Get the registered class
if ( isset($this->values[$key])){
return $this->values[$key];
}
return null;
}
function set( $key,$value){//Registration class method
$this->values[$key]=$value;
}
}
$reg=registry::instance();
$reg->set("website",new webSite("WEB Development Notes","www.chhua.com"));//Register the class
$website=$reg-> get("website");//Get the class
echo $website->getName();//Output WEB development notes
echo $website->getUrl();//Output www.chhua. com
?>

The function of the registry is to provide system-level object access functions. Some students will say that this is unnecessary, but there is indeed no need to register classes in small projects. If it is a large project, it is still very useful.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325018.htmlTechArticleI have also written a registry class before, but that one cannot register multiple classes. I use an array below. Classes are stored. Copy the code. The code is as follows: ?php //Basic class class webS...
Statement:
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