


Understand the proxy pattern of PHP design patterns in one article
The proxy pattern is a structural design pattern, targeting the classic structure in which classes and objects are combined. The proxy pattern is also a frequently used design pattern that we need to focus on. It can add some additional functions without changing the target object.
Definition
Proxy mode (Proxy) provides a proxy for other objects to control access to this object. Use the proxy pattern to create a proxy object, let the proxy object control access to the target object (the target object can be a remote object, an object that is expensive to create, or an object that requires security control), and you can add some extras without changing the target object. function.
Question
Currently, the system has a Login class for user login and registration business. The pseudo code is as follows:
class UserLogin { // …… 省略属性和部分方法 public function login ($name, $pass) { // 登录业务 } public function reg ($name, $pass) { // 注册业务 } }
Now, we want to add a function to the user login and registration business - current limiting, so that the client can limit the frequency of calling this method to a maximum of 5 times per second. Now, let's implement this function. The pseudo code is as follows:
class UserLogin { // …… 省略属性和部分方法 public function login ($name, $pass) { // 限流 $limit = new Limit(); if ($limit->restrict()) { // …… } // 登录业务 } public function reg ($name, $pass) { // 限流 $limit = new Limit(); if ($limit->restrict()) { // …… } // 注册业务 } }
Let's take a look at the above code. It has several problems. First, the current limiting code invades the business code and is highly coupled with the business code. Secondly, current limiting has nothing to do with business code and violates the single responsibility principle.
Implementation
Next, we rewrite the above code using proxy mode. The rewritten code is as follows:
interface IUserLogin { function login (); function register (); } class UserLogin implements IUserLogin { // …… 省略属性和部分方法 public function reg ($uname, $pass) { // 注册业务 } public function login ($uname, $pass) { // 登录业务 } } class UserLoginProxy implements IUserLogin { private $limit = null; private $login = null; public function __construct(Limit $limit, Login $login) { $this->limit = $limit; $this->login = $login; } public function login($uname, $pass) { if ($this->limit->restrict()) { // ... } return $this->login->login($uname, $pass); } public function register($uname, $pass) { if ($this->limit->restrict()) { // ... } return $this->login->register($uname, $pass); } }
The above method is based on the design idea of interface rather than implementation programming, but if the original class does not define an interface, or this class is not developed and maintained by us, then how to implement the proxy mode?
For the extension of this external class, we generally use the inheritance method to achieve it.
class UserLogin { public function reg ($uname, $pass) { // 注册业务 } public function login ($uname, $pass) { // 登录业务 } } class UserLoginProxy extends Login { private $limit = null; public function __construct(Limit $limit, Login $login) { $this->limit = $limit; $this->login = $login; } public function login($uname, $pass) { if ($this->limit->restrict()) { // ... } return parent::login($uname, $pass); } public function reg($uname, $pass) { if ($this->limit->restrict()) { // ... } return parent::register($uname, $pass); } }
Take a look at the above code. Are there any problems? You will find that this similar code
if ($this->limit->restrict()) { // ... }
appears twice. Now we just add the current limiting function to two methods. If the UserLogin class has 10 methods, and we want to add the current limiting function to each method, then we need to copy this code 10 times. If we want to add current limiting function to all methods in 10 classes, and each class has 10 methods, then the above current limiting code will be repeated 100 times.
Of course, you will say that I can encapsulate the current limiting code into a function to solve the above problem? But there is still a problem that cannot be solved. Every method in the original class must be re-implemented in the proxy class. Just like there are reg and login methods in the original class above, there are also reg and login methods in the proxy class.
Dynamic proxy
How to solve the above problems, we can use dynamic proxy to solve it. If you want to use dynamic proxy, you must understand and use the reflection mechanism in PHP.
php has a complete reflection API, adding the ability to reverse engineer classes, interfaces, functions, methods and extensions. Additionally, the Reflection API provides methods to extract documentation comments from functions, classes, and methods. Regarding the knowledge related to PHP reflection, I will not go into details here. You can check the relevant information by yourself.
Note that using reflection consumes a lot of performance, so please do not use it under normal circumstances.
Let’s show how to use reflection to implement dynamic proxy. The pseudo code is as follows:
class UserLogin { public function reg ($uname, $pass) { // 注册业务 echo '注册业务' . PHP_EOL; } public function login ($uname, $pass) { // 登录业务 echo '登录业务' . PHP_EOL; } } class LimitProxy { // 用来保存多个实例对象 private $target = []; public function __construct(Object $obj) { $this->target[] = $obj; } public function __call($name, $arguments) { foreach ($this->target as $obj) { $ref = new \ReflectionClass($obj); if ($method = $ref->getMethod($name)) { if ($method->isPublic() && !$method->isAbstract()) { // 限流 echo "这里是限流业务处理" . PHP_EOL; $result = $method->isStatic() ? $method->invoke(null, $obj, ...$arguments) : $method->invoke($obj, ...$arguments); return $result; } } } } }
The test code is as follows:
$login = new Login(); $loginProxy = new LimitProxy($login); $loginProxy->reg('gwx', '111111'); $loginProxy->login('james', '111111111');
Application scenario
Access control (protection agent). For example, the system has an order module. Originally, this module also had permission control, but now we want it to be accessible only to clients with specified IP addresses. Then we can use proxy mode.
Local execution of remote services (remote proxy) is suitable for situations where the service object is located on a remote server.
Develop some non-functional requirements in the business code, such as: current limiting, statistics, logging
Cache applications, For example, add a cache proxy. When the cache exists, the cache proxy is called to obtain the cached data. When the cache does not exist, the original interface is called.
The above is the detailed content of Understand the proxy pattern of PHP design patterns in one article. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator
Generate AI Hentai for free.

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



Many Windows 11 users have reported seeing the error message "Error writing proxy settings - Access is denied" during system startup or when trying to execute commands through the command prompt. Usually, this error occurs when trying to run any command or after upgrading to Windows operating system, some improperly installed 3rd party applications may interfere with the command prompt. Are you troubled by this error writing proxy settings message on your Windows 11 computer? Then you've arrived at the right place. In this article, we have curated some possible solutions that can help you resolve this issue on your computer. Fix 1 – Change Windows’ Default Terminal Application Are You Starting from Windo

The proxy pattern is a Java framework design pattern that mediates between the client and the target object by creating a proxy object. Its advantages include: protecting target objects, providing data integrity and security; controlling access to the target, implementing permission control and security measures; enhancing target behavior, adding additional functions such as logging, caching and transaction management; simplifying testing and facilitating mocking and stubbing goals. However, the proxy pattern also has disadvantages: Overhead: Creating and maintaining proxy objects may reduce performance; Complexity: Requires a deep understanding of the design pattern; Restricted access to targets, which may not be appropriate in some cases.

Introduction PHP design patterns are a set of proven solutions to common challenges in software development. By following these patterns, developers can create elegant, robust, and maintainable code. They help developers follow SOLID principles (single responsibility, open-closed, Liskov replacement, interface isolation and dependency inversion), thereby improving code readability, maintainability and scalability. Types of Design Patterns There are many different design patterns, each with its own unique purpose and advantages. Here are some of the most commonly used PHP design patterns: Singleton pattern: Ensures that a class has only one instance and provides a way to access this instance globally. Factory Pattern: Creates an object without specifying its exact class. It allows developers to conditionally

PHP is a widely used and very popular programming language. PHP is a very important part of today's web applications. Design patterns play a vital role in developing PHP applications. Design pattern is a template for solving problems that can be reused in different environments. It helps us write better code and make the code more reliable, maintainable and scalable. In this article, we will explore some commonly used design patterns in PHP and how to implement them. Singleton Pattern Singleton pattern is a creation pattern that allows

Proxy pattern in Java Proxy is a design pattern that allows an object to appear in the form of another object and access the original object through the proxy object. Proxies are widely used in many applications, the most common of which is to implement remote object calls and logging in the network. There are many examples of using the proxy pattern in Java as well. The proxy pattern in Java is mainly implemented in the following three ways: Static proxy Static proxy is implemented by creating a proxy class during the compilation phase. Proxy class and original class implementation

PHP design patterns include: 1. Singleton mode, which ensures that a class has only one instantiated object; 2. Factory mode, which encapsulates the instantiation process of the object in a factory class; 3. Abstract factory mode, which is similar to a factory Pattern of creating objects; 4. Observer pattern, realizing one-to-many dependencies between objects; 5. Adapter pattern, converting the interface of one class into the interface of another class; 6. Decorator pattern, dynamically Add some additional functions to an object; 7. Iterator pattern; 8. Strategy pattern; 9. Template method pattern, etc.

Introduction to PHP core design patterns and practices: Design patterns are commonly used problem-solving templates in software development. They provide a reusable solution that can help us follow best practices and good software design principles during the development process. . As a widely used programming language, PHP also has many common and useful design patterns that can be used in core development. This article will introduce several common PHP design patterns and provide relevant code examples. 1. Singleton mode (Singleton) Singleton mode is a type that only allows

With the continuous development of technology, design patterns are becoming more and more important in software development. As the latest PHP version, PHP7.0 also integrates many design patterns. In this article, we will explore the design patterns in PHP7.0 to help PHP programmers better understand and apply these patterns. Singleton pattern The singleton pattern is a creational pattern that ensures that a class has only one instance and provides a global access point. In PHP7.0, you can use the __construct method and static method to
