Registry mode can seem to think of him as a global variable, all the modules from this global variable access data, or can also be imagined as a bar of the wishing wall or message version, the above content can be seen, can also be rewritten. There are three categories of registry classes ( request level, session level, application level) that are mainly covered by scope.
namespace Woo\base;
Base classAbstract classRegistry {Abstract protected functionGet$key); Abstract protected functionSet$key,$val);}//request level, his life cycle is usually a request from a user to the background program to reply to the requestsclassRequestregistryextendsregistry{Private $values=Array(); Private Static $instance; Private function__construct () {}
Static functioninstance () {//Singleton, that is, there is only one instance of this classif(!isset(Self::$instance) ) { self::$instance=NewSelf (); } returnSelf::$instance; } protected functionGet$key){ if(isset($this->values[$key]){ return $this->values[$key]; } return NULL; } protected functionSet$key,$val){ $this->values[$key] =$val; } Static functiongetrequest () {returnSelf::instance ()->get (' request ')); } Static functionSetrequest (\woo\controller\request$request{//\woo\controller\request primary function is a class that processes user request informationreturnSelf::instance ()->set (' request ',$request); }}//session level, the lifetime of the class in this example is mostly the time to see the session's lifetime classSessionregistryextendsregistry{Private Static $instance; Private function__construct () {Session_Start(); } Static functioninstance () {if(!isset(Self::$instance) ) { self::$instance=NewSelf (); } returnSelf::$instance; } protected functionGet$key){ if(isset($_session[__class__][$key])){ return $_session[__class__][$key]; } return NULL; } protected functionSet$key,$val){ $_session[__class__][$key] =$val; } functionSetcomplex (Complex$complex) { self:: Instance ()->set (' Complex ',$complex); } functionGetcomplex () {returnSelf::instance ()->get (' complex ')); }}//application level, in this example, because the associated value is saved in a text file, the saved value persists as long as the file exists classApplicationregistryextendsregistry{Private Static $instance; Private $freezedir= ' Data '; Private $values=Array(); Private $mtimes=Array(); Private function__construct () {}Static functioninstance () {if(!isset(Self::$instance) ) { self::$instance=NewSelf (); } returnSelf::$instance; } protected functionGet$key){ $path=$this->freezedir. Directory_separator.$key;//The path to the file where the value is savedif(file_exists($path)){ Clearstatcache(); Clears the file modification time of the last record of the filemtime cache$mtime=Filemtime($path); if(!isset($this->mtimes[$key])){ $this->mtimes[$key] = 0; } if($mtime>$this->mtimes[$key] {//the contents of the file should be re-retrieved if it has been modified .$data=file_get_contents($path); $this->mtimes[$key] =$mtime; return($this->values[$key] =unserialize($data)); } } if(isset($this->values[$key])){ return $this->values[$key]; } return NULL; } protected functionSet ($key,$val){ $this->values[$key] =$val; $path=$this->freezedir. Directory_separator.$key; file_put_contents($path,Serialize($val)); $this->mtimes[$key] = Time(); } Static functionGetdsn () {returnSelf::instance ()->get (' DSN '); } Static functionSETDSN ($dsn){ returnSelf::instance ()->set (' DSN ',$dsn); } }
PHP Object-Oriented registry schema