PHP object-oriented registry mode, php object-oriented Registry

Source: Internet
Author: User

PHP object-oriented registry mode, php object-oriented Registry

Registry mode may be like a global variable. All modules access data from this global variable, or they can be imagined as the wishing wall or message board of a bar, you can see or rewrite the content above. Here we mainly introduce three types of registry classes (Request level, session level, and application level) by scope ).


Namespace woo \ base;
// Base class abstract class Registry {abstract protected function get ($ key); abstract protected function set ($ key, $ val);} // request level, the life cycle is usually from when a user initiates a request to when the background program replies this request. class RequestRegistry extends Registry {private $ values = array (); private static $ instance; private function _ construct (){}
Static function instance () {// Singleton, that is, this class has only one unique 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 \ contr The main function of oller \ Request is to process user request information. return self: instance ()-> set ('request', $ request) ;}// session level, in this example, the class life cycle mainly depends on the SESSION life time 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: insta Nce ()-> get ('compute') ;}// application level. In this example, if the file exists, the saved value also exists in the 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; // path of the file storing the value if (file_exists ($ path) {clearstatcache (); // delete the file modification time of the last record cached by filemtime $ mtime = filemtime ($ path); if (! Isset ($ this-> mtimes [$ key]) {$ this-> mtimes [$ key] = 0 ;} if ($ mtime> $ this-> mtimes [$ key]) {// if the file content has been modified, the value $ 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 );}}

 

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.