


An in-depth analysis of the proxy pattern of PHP design patterns_PHP tutorial
Proxy mode (Proxy), which is an enhancement to a simple handler (or pointer), is used to reference an object: this pointer is replaced by a proxy (Proxy) object, which is located between the client and the real executor In between, pointers have a hook that can be exploited by multiple targets.
Technically speaking, this pattern inserts a proxy object between the client and the real subject (RealSubject), maintaining the subject interface and delegating its methods in different ways. Agents can do everything transparently: lazily create RealSubjects or load data, exchange messages with other machines, copy-on-write policies, etc. This is somewhat similar to an HTTP proxy, where clients (such as browsers) and applications rely on contact with the HTTP server. The proxy can complete other tasks while managing the connection, such as access control and caching large download files.
The object graph of the proxy mode is similar in structure to the object graph of the decoration mode, but the purpose of expression is different. The decorator dynamically adds behavior to the object, while the proxy controls access from the client. Additionally, the agent only creates RealSubjects when needed.
Participants:
◆Client: depends on the subject implementation;
◆Subject: abstraction of RealSubject;
◆RealSubject: Complete costly work or contain a large amount of data;
◆Proxy (Proxy): Provide a consistent reference to the Subject for the Client, and only create a RealSubject instance or interact with the RealSubject when needed Instance communication.
The following are two widely used examples of proxy patterns:
1. Object-relational mapping (Orms) creates proxies as subclasses of entity classes at runtime , to implement lazy loading (virtual proxy), this proxy will cover all entity methods, append a loader in front, and will not contain any data before the method is actually called. Orms proxy supports bidirectional relationships between objects, without loading the entire Databases because they are placed at the boundaries of the currently loaded object graph.
2. Java RMI uses remote proxy objects (remote proxies). When their methods are called, the proxy serializes the parameters, performs the request on the network, and delegates the call to the real object on another node. This technology allows transparency You can call remote objects without worrying about whether they are on the same machine, but this transparency can easily slow down execution.
The following code example implements an ImageProxy to defer the loading of image data.
/**
* Subject interface.
* Client depends only on this abstraction.
*/
interface Image
{
public function getWidth();
public function getHeight();
public function getPath();
/**
* @return string the image's byte stream
*/
public function dump();
}
/**
* Abstract class to avoid repetition of boilerplate code in the Proxy
* and in the Subject. Only the methods which can be provided without
* instancing the RealSubject are present here.
*/
abstract class AbstractImage implements Image
{
protected $_width;
protected $_height;
protected $_path;
protected $_data;
public function getWidth()
{
return $this->_width;
}
public function getHeight()
{
return $this->_height;
}
public function getPath()
{
return $this->_path;
}
}
/**
* The RealSubject. Always loads the image, even if no dump of the data
* is required.
*/
class RawImage extends AbstractImage
{
public function __construct($path)
{
$this->_path = $path;
list ($this->_width, $this->_height) = getimagesize($path);
$this->_data = file_get_contents($path);
}
public function dump()
{
return $this->_data;
}
}
/**
* Proxy. Defers loading the image data until it becomes really mandatory.
* This class does its best to postpone the very expensive operations
* such as the actual loading of the BLOB.
*/
class ImageProxy extends AbstractImage
{
public function __construct($path)
{
$this->_path = $path;
list ($this->_width, $this->_height) = getimagesize($path);
}
/**
* Creates a RawImage and exploits its functionalities.
*/
protected function _lazyLoad()
{
if ($this->_realImage === null) {
$this->_realImage = new RawImage($this->_path);
}
}
public function dump()
{
$this->_lazyLoad();
return $this->_realImage->dump();
}
}
/**
* Client class that does not use the data dump of the image.
* Passing blindly a Proxy to this class and to other Clients makes sense
* as the data would be loaded anyway when Image::dump() is called.
*/
class Client
{
public function tag(Image $img)
{
return ';
}
}
$path = '/home/giorgio/shared/Immagini/kiki.png';
$client = new Client();
$image = new RawImage($path); // loading of the BLOB takes place
echo $client->tag($image), "n";
$proxy = new ImageProxy($path);
echo $client->tag($proxy), "n"; // loading does not take place even here
The above code implements PHP’s proxy mode. Simply put, the proxy pattern is to provide a proxy for other objects to control access to this object.

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



In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.
