This article mainly introduces the detailed explanation of PHP object-oriented registry mode. Interested friends can refer to it. I hope it will be helpful to everyone.
The registry model can be imagined as a global variable, and all modules access data from this global variable, or it can also be imagined as a wishing wall or message board in a bar, with the content above Everyone can see it and can rewrite it. Here we mainly introduce three categories of registry classes (request level, session level, application level) according to scope.
namespace woo\base; //基类 abstract class Registry { abstract protected function get($key); abstract protected function set($key,$val); } //请求级别,他的生存周期通常为从用户发起一个请求到后台程序回复这个请求为止 class RequestRegistry extends Registry{ private $values = array(); private static $instance; private function __construct (){} static function instance(){ // 单例,即这个类只有一个唯一的实例 if(!isset(self::$instance)){ self::$instance = new self(); } return self::$instance; } protected function get($key){ if(isset($this->values[$key]){ return $this->values[$key]; } return null; } protected function set($key,$val){ $this->values[$key] = $val; } static function getRequest(){ return self::instance()->get('request'); } static function setRequest(\woo\controller\Request $request){ //\woo\controller\Request 主要功能是处理用户请求信息的一个类 return self::instance()->set('request',$request); } } //会话级别,此示例中类的生存周期主要还是看SESSION的生存时间 class SessionRegistry extends Registry{ private static $instance; private function __construct (){ session_start(); } static function instance(){ if(!isset(self::$instance)){ self::$instance = new self(); } return self::$instance; } protected function get($key){ if(isset($_SESSION[__CLASS__][$key])){ return $_SESSION[__CLASS__][$key]; } return null; } protected function set($key,$val){ $_SESSION[__CLASS__][$key] = $val; } function setComplex(Complex $complex){ self::instance()->set('complex',$complex); } function getComplex(){ return self::instance()->get('complex'); } } //应用程序级别,此示例中因相关的值是保存在文本文件中,所以只要文件存在,保存的值也就一直存在 class ApplicationRegistry extends Registry{ private Static $instance; private $freezedir = 'data'; private $values = array(); private $mtimes = array(); private function __construct (){} static function instance(){ if(!isset(self::$instance)){ self::$instance = new self(); } return self::$instance; } protected function get($key){ $path = $this->freezedir . DIRECTORY_SEPARATOR . $key; // 保存值的文件的路径 if(file_exists($path)){ clearstatcache(); // 清除filemtime缓存的上次记录的文件修改时间 $mtime = filemtime($path); if(!isset($this->mtimes[$key])){ $this->mtimes[$key] = 0; } if($mtime > $this->mtimes[$key]){ // 文件内容假如被修改过,那么就要重新获取里面的值 $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 function set ($key,$val){ $this->values[$key] = $val; $path = $this->freezedir . DIRECTORY_SEPARATOR . $key; file_put_contents($path,serialize($val)); $this->mtimes[$key] = time(); } static function getDSN(){ return self::instance()->get('dsn'); } static function setDSN($dsn){ return self::instance()->set('dsn',$dsn); } }
Related recommendations:
AngularJSRegistryDetailed steps for single verification
How to use regular expressions to verify registry
Detailed example of vue composite component implementationRegistrySingle function
The above is the detailed content of Detailed explanation of PHP object-oriented registry mode. For more information, please follow other related articles on the PHP Chinese website!