About Registry Mode

Source: Internet
Author: User
Tags dsn session id
Usually the application can be divided into the following levels:
1. Front-end View
2. Command control
3. Business logic
4. Data processing
The front-end view is responsible for displaying the user data and collecting user input data and submitting it to the command control hierarchy. Command control layer receives data for a series of processing, entrusted to the business logic layer to complete the specific tasks. The business logic layer invokes the data processing module to complete the storage of user data.
But how is the data submitted by the front-end directly and appropriately delivered at several levels? One is passed by a context class provided in the preceding command pattern, the parameters are placed inside the context object, passed from the command control layer to the business logic layer, and the result of the operation is returned through the context after a series of operations are completed. The second way is to modify the interface of the command object to match the corresponding data transfer. Both of these methods can sometimes break the encapsulation. We know that Singleton mode provides another way to access global variables. Static variables are locally hidden and can be easily set through an interface to get object properties. The registration Model (REGISTRY) is taking advantage of this convenience.

The registration mode can also be considered as a single-instance version of the context object.

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 lifecycles: One is to start with receiving an HTTP request and the request is finished. The other is a session-level object that can store object data in the session and PHP
Session_Start to restore different object data based on the session ID stored in the cookie, you can achieve the same user multiple requests to access the same object data. There is also an application-scoped level. That is, the same application
Different users can share the same object data, which requires PHP's built-in serialization function to complete.


Data registration mode based on one HTTP request:

Class Requestregistry extends registry{private static $instance;p rivate $values = Array ();p rivate 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 ');}}
Registry of Session requests:


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 ');}}
Support for application-level registries:


Class Applicationregistry extends Registry{private $dir = "Data";p rivate static $instance;p rivate $values = Array ();p Riva Te $mtimes = array ();p rivate 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 describes the registry schema, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.