Home >Backend Development >PHP Tutorial >PHP Design Pattern Registry Pattern_PHP Tutorial

PHP Design Pattern Registry Pattern_PHP Tutorial

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

The following is the code for the basic registry class:

Copy the code The code is as follows:

class Registry {
private static $instance;
private $request;//Content class of the registry
private function __construct(){}//This class cannot be instantiated
static function instance() {//Singleton class, return the instance through this method
if (!isset(self::$instance)){self::$instance=new self();}
return self::$instance;
}
function getRequest(){//Return the registered content class
return $this->request;
}
function setRequest(request $request){//Set the registered Content class
$this->request=$request;
}
}
class request{//Registered class
private $webname="WEB Development Notes";
private $url="www.chhua.com";
function getName(){
echo $this->url;//Output www.chhua.com
}
}// Registered empty class
//Use
$reg=Registry::instance();
$reg->setRequest(new request());
$request=$reg-> ;getRequest();
$request->getName();//Output www.chhua.com
?>

The role of the registry is to provide system-level objects Access features.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325015.htmlTechArticleThe following is the code of the basic registry class: Copy the code as follows: ?php class Registry { private static $instance ; private $request;//The content class of the registry private function __con...
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