PHP architecture registry
What is registry mode? it's actually really easy!
The role of the registry is to provide system-level object access functions. We often take "global variables are bad" as an article of faith when coding. However, everything has two sides, and global data access is very attractive.
Here comes the problem:
Most systems are divided into several layers, and each layer only communicates with adjacent layers through pre-defined channels. Sharing layers makes the program flexible, and replacing or modifying each layer can minimize the impact on other parts of the system. But what about when you need information in one layer that is not adjacent to another layer?
Option 1: Pass contextual information from one object to another required object through the connection between the layers of the system: pass this information from one object to another object in the system, from a controller responsible for processing the request The object is passed to the object of the business logic layer, and then passed to the object responsible for talking to the database. Of course, you can also pass the ApplicationHelper object, or a specific Context object.
Option 2: The interfaces of all objects must be modified to determine whether the context objects are what they need. Obviously, sometimes this approach breaks loose coupling.
Option 3: Through registry mode. Registry classes provide static methods (or instantiation methods of singleton objects) to allow other objects to access the data in them (usually objects). Every object in the entire system can access these data objects.
Think about the scope of PHP before implementing it:
Scope is usually used to describe the visible program of an object or value in the code. The life cycle of a variable can be measured in time. There are 3 levels of variable scope.
1. HTTP request scope
refers to the cycle from the beginning to the end of an HTTP request.
2. Session scope
PHP has built-in support for session variables. At the end of a request, the session variables are serialized and stored in the file system or database, and then retrieved at the beginning of the next request. The session ID stored in the cookie and passed in the query string are used to track the owner of the session. Therefore, you can think of certain variables as having session-level scope. Using this, you can store objects between several requests and save traces of user access to the database. Of course, be careful not to hold different versions of the same object, so when you save a session object to the database, you need to consider using certain locking strategies.
3. Application Scope
In other languages, especially JAVA, there is a cache pool, which is the concept of "application scope". Variables in memory can be accessed by all object instances in the program. PHP does not have such functionality, but in large-scale applications it can be useful to access application-level data in order to access configuration variables.
The following uses the registry to implement these three scopes. The class diagram is as follows:
<?php namespace woo\base; require_once( "woo/controller/AppController.php"); 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 ) { return self::instance()->set('request', $request ); } } 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 = "/tmp/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(); $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); } static function setControllerMap( \woo\controller\ControllerMap $map ) { self::instance()->set( 'cmap', $map ); } static function getControllerMap() { return self::instance()->get( 'cmap' ); } static function appController() { $obj = self::instance(); if ( ! isset( $obj->appController ) ) { $cmap = $obj->getControllerMap(); $obj->appController = new \woo\controller\AppController( $cmap ); } return $obj->appController; } } //如果你安装了PHP的shm扩展,就可以使用该扩展中函数来实现应用程序注册表 class MemApplicationRegistry extends Registry { private static $instance; private $values=array(); private $id; const DSN=1; private function __construct() { $this->id = @shm_attach(55, 10000, 0600); if ( ! $this->id ) { throw new Exception("could not access shared memory"); } } static function instance() { if ( ! isset(self::$instance) ) { self::$instance = new self(); } return self::$instance; } protected function get( $key ) { return shm_get_var( $this->id, $key ); } protected function set( $key, $val ) { return shm_put_var( $this->id, $key, $val ); } static function getDSN() { return self::instance()->get(self::DSN); } static function setDSN( $dsn ) { return self::instance()->set(self::DSN, $dsn); } } ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The Xbox app allows its users to play exciting games on their PC in case the console is not there. Users can connect their Xbox controller directly to the PC and start playing on their Windows PC. Sometimes the Xbox Wireless Controller cannot be removed from a user's Windows 11/Windows 10 laptop. There are a number of issues, the most prominent of which is users encountering "removal failed" issues when trying to remove gadgets. If you get a "Removal failed" error when removing your Xbox Wireless Controller, restart your computer. The most straightforward solution to common problems is to restart your Windows device. If you are restarting your computer

Some of our readers have recently reported that they received one of the Windows activation errors, such as the 0xC004D302 error code on their Windows PC, due to which certain features stopped working. This error occurs on computers running non-core versions of Microsoft Windows. It can be triggered when the trusted data store is reactivated. You can solve this problem by following this article. What causes the 0xc004d302 error in this article? Here are some potential reasons behind the 0xC004d302 Windows Activation Error Code: Fake or Incorrect Product Key – One of the most common reasons why you receive the 0xC004d302 error code is when you enter

Folder properties provide users with details such as creation date, folder name, size, etc. However, sometimes you may come across the Location tab in the properties of a Windows folder. So, we'll walk you through the steps to troubleshoot and re-enable it. Alternatively, you can check out our detailed guide on fixing folder access denied issue on Windows 11. Why can't I find the Location tab in the folder properties? There are several factors that can cause the Location tab to be missing in folder properties on Windows 11. Most notably: Folders don’t support moving – some folders support moving to another location, while others don’t. By default, moving from one location to another is supported

Removing a Printer on Windows 11 Completely removing a printer requires two things - removing the printer itself, and then removing the driver. There are several ways you can perform these operations on Windows 11. They are: Method 1: From Settings Perhaps the most common method, a printer can be easily removed from the Windows Settings application. Here's how to do it: Press Win+I to open Settings. Then click Bluetooth & Devices in the left pane. On the right, click Printers & Scanners. Click the printer you want to uninstall. Select Delete. When prompted, click Yes. Method 2: Use Device Manager to Remove Printer Drivers Device Manager lets you manage and remove Windows

Location services are an important feature for some applications on Windows, depending on the location of your system. So if you see the Location Services option grayed out on your system, you can't even toggle it manually. Usually, some system glitch causes this issue and you can easily fix it by following these simple solutions. Fix 1 – Adjust the registry You can adjust the registry to fix this greyed out location services issue. 1. Press the Win key and type "Registry". 2. Then, click "Registry Editor" to open the Registry Editor on your system. 3. You will delete a specific registry subdirectory. Usually, it won't affect your system at all. However, it is recommended that you perform a system backup. one. After opening the Registry Editor, click

If you are a Windows user and want to disable the core parking functionality in your system, this article will guide you through the process. What is core parking? The core parking function is a power saving mechanism. It puts some of your processors into a sleep mode that doesn't perform any tasks and consumes very little or no power. This helps reduce energy consumption and therefore heat in the system. These cores are unparked when needed. Few users need to disable this feature, especially gamers. Disabling this feature will improve your system performance. Disable Windows Core Parking using Windows Registry Editor Step 1: Use Windows + R keys simultaneously to open the run prompt. Step 2: At the run prompt

If WDMyCloud is not showing up on the network in Windows 11, this can be a big problem, especially if you store backups or other important files in it. This can be a big problem for users who frequently need to access network storage, so in today's guide, we'll show you how to fix this problem permanently. Why doesn't WDMyCloud show up on Windows 11 network? Your MyCloud device, network adapter, or internet connection is not configured correctly. The SMB function is not installed on the computer. A temporary glitch in Winsock can sometimes cause this problem. What should I do if my cloud doesn't show up on the network? Before we start fixing the problem, you can perform some preliminary checks:

Jump list on Windows provides easy access to recent files. If you want to add jump list items on Windows, this tutorial will help you get started. Jump Lists on Windows 10 and Windows 11 allow easy access to recently used files, folders, and websites. However, you may want to display more than the 10 most recent items that Windows displays by default. You can make Windows display more items in your jump list by adjusting the registry. For example, you can set it to display about 15-20 items, which will display well on most monitors. This tutorial will show you how to use Windows 10 and Windows 10
