PHP Object-Oriented registry schema

Source: Internet
Author: User
Tags dsn

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.