--- url: "https://refactoring.guru/design-patterns/proxy/php/example" titulek: "Proxy in PHP / Design Patterns" h1: "Proxy in PHP" popis: "Proxy pattern in PHP. Full code example in PHP with detailed comments and explanation. Proxy is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/proxy/php/example" drobecky: ["Home", "Design Patterns", "Proxy", "PHP"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 22:22:19" znaku: 7348 slov: 1104 kodovani: "utf-8" sha256: "dff4856a436b6543fc70d9284fcf20cf41861c305bc891cf410085d62d1f923d" --- # **Proxy** in PHP **Proxy** is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object. The proxy object has the same interface as a service, which makes it interchangeable with a real object when passed to a client. [Learn more about Proxy](/design-patterns/proxy) **Complexity:** **Popularity:** **Usage examples:** While the Proxy pattern isn’t a frequent guest in most PHP applications, it’s still very handy in some special cases. It’s irreplaceable when you want to add some additional behaviors to an object of some existing class without changing the client code. **Identification:** Proxies delegate all of the real work to some other object. Each proxy method should, in the end, refer to a service object unless the proxy is a subclass of a service. ## Conceptual Example This example illustrates the structure of the **Proxy** design pattern and focuses on the following questions: - What classes does it consist of? - What roles do these classes play? - In what way the elements of the pattern are related? After learning about the pattern’s structure it’ll be easier for you to grasp the following example, based on a real-world PHP use case. #### **index.php:** Conceptual example ``` realSubject = $realSubject; } /** * The most common applications of the Proxy pattern are lazy loading, * caching, controlling the access, logging, etc. A Proxy can perform one of * these things and then, depending on the result, pass the execution to the * same method in a linked RealSubject object. */ public function request(): void { if ($this->checkAccess()) { $this->realSubject->request(); $this->logAccess(); } } private function checkAccess(): bool { // Some real checks should go here. echo "Proxy: Checking access prior to firing a real request.\n"; return true; } private function logAccess(): void { echo "Proxy: Logging the time of request.\n"; } } /** * The client code is supposed to work with all objects (both subjects and * proxies) via the Subject interface in order to support both real subjects and * proxies. In real life, however, clients mostly work with their real subjects * directly. In this case, to implement the pattern more easily, you can extend * your proxy from the real subject's class. */ function clientCode(Subject $subject) { // ... $subject->request(); // ... } echo "Client: Executing the client code with a real subject:\n"; $realSubject = new RealSubject(); clientCode($realSubject); echo "\n"; echo "Client: Executing the same client code with a proxy:\n"; $proxy = new Proxy($realSubject); clientCode($proxy); ``` #### **Output.txt:** Execution result ``` Client: Executing the client code with a real subject: RealSubject: Handling request. Client: Executing the same client code with a proxy: Proxy: Checking access prior to firing a real request. RealSubject: Handling request. Proxy: Logging the time of request. ``` ## Real World Example There are countless ways **proxies** can be used: caching, logging, access control, delayed initialization, etc. This example demonstrates how the Proxy pattern can improve the performance of a downloader object by caching its results. #### **index.php:** Real world example ``` downloader = $downloader; } public function download(string $url): string { if (!isset($this->cache[$url])) { echo "CacheProxy MISS. "; $result = $this->downloader->download($url); $this->cache[$url] = $result; } else { echo "CacheProxy HIT. Retrieving result from cache.\n"; } return $this->cache[$url]; } } /** * The client code may issue several similar download requests. In this case, * the caching proxy saves time and traffic by serving results from cache. * * The client is unaware that it works with a proxy because it works with * downloaders via the abstract interface. */ function clientCode(Downloader $subject) { // ... $result = $subject->download("http://example.com/"); // Duplicate download requests could be cached for a speed gain. $result = $subject->download("http://example.com/"); // ... } echo "Executing client code with real subject:\n"; $realSubject = new SimpleDownloader(); clientCode($realSubject); echo "\n"; echo "Executing the same client code with a proxy:\n"; $proxy = new CachingDownloader($realSubject); clientCode($proxy); ``` #### **Output.txt:** Execution result ``` Executing client code with real subject: Downloading a file from the Internet. Downloaded bytes: 1270 Downloading a file from the Internet. Downloaded bytes: 1270 Executing the same client code with a proxy: CacheProxy MISS. Downloading a file from the Internet. Downloaded bytes: 1270 CacheProxy HIT. Retrieving result from cache. ``` ## **Proxy** in Other Languages