About Registry mode

Source: Internet
Author: User
: This article mainly introduces the registry mode. if you are interested in the PHP Tutorial, you can refer to it. Generally, applications can be divided into the following layers:
1. front-end view
2. Command Control
3. Business Logic
4. Data processing
The front-end view displays user data and collects user input data, and submits the data to the command control layer. The command control layer receives data for a series of processing and entrusts the business logic layer to complete specific tasks. The business logic layer calls the data processing module to store user data.
But how can we directly and appropriately transmit the data submitted by the front-end at several levels? One is to pass the context class provided by the preceding command mode, place the parameters in the context object, and transfer the parameters from the command control layer to the business logic layer, after completing a series of operations, return the operation results through context. The second method is to modify the interface of the command object to adapt to the corresponding data transmission. These two methods sometimes break the encapsulation. We know that the singleton mode provides another way to access global variables. Static variables are hidden locally and you can easily set and obtain object attributes through the interface. Registry makes use of this convenience.

Here we can also regard the registration mode as the context object of the Singleton version.

A simple Registry implementation:

abstract class Registry{abstract protected function get($key);abstract protected function set($key, $value);}
PHP supports three types of object data lifecycle: One is that the request is processed after an HTTP request is received. The other is the support for session-level objects, that is, the object data can be stored in the session.
When session _ start is used, different object data is restored based on the session ID stored in cookies. This allows the same user to access the same object data multiple times. There is also an application scope level. That is,
Different users can share the same object data. this operation requires PHP built-in serialization functions.


Data registration mode based on an HTTP request:

class RequestRegistry extends Registry{private static $instance;private $values = array();private function __construct(){}static public 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, $value){$this->values[$key] = $value;}static function set_request(Request $request){self::$instance->set('request', $request);}static function get_request(){return self::$instance->get('request');}}
Session Request Registry:


class SessionRegistry extends Registry{private $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, $value){$_SESSION[__CLASS__][$key] = $value;}public function set_complex(Complex $complex){self::$instance->set('complex', $complex);}public function get_complex(){return self::$instance->get('complex');}}
Supports application-level registries:


class ApplicationRegistry extends Registry{private $dir = "data";private static $instance;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 set($key, $value){$this->values[$key] = $value;$path = $this->dir . DIRECTORY_SEPARATOR . $key;file_put_contents($path, serialize($value));$this->mtimes[$key] = time();}protected function get($key){$path = $this->dir . DIRECTORY_SEPARATOR . $key;if(file_exists($path)){$mtime = filetime($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;}static function get_dsn(){return self::$instance->get('dsn');}static function set_dsn($dsn){self::$instance->set('dsn', $dsn);}}
The end.


The above introduces the registry mode, including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.