Author - StudySection Post Views - 510 views
Proxy Pattern in PHP

How to use Proxy Pattern in PHP

The proxy model is a way to create an intermediary between your client/controller and your disciplinary class. Think of an HTTP proxy, which is a gateway between your browser (client) and a server (subject). These proxies can be used to enhance the user experience by caching data, removing advertisements, allowing access to restricted content, and much more. The proxy model has similar objectives and therefore can be used to simplify, optimize and improve existing subjects (classes).

When to use the proxy model

One of the main use cases of optimization for the proxy model in PHP is the lazy initialization of the subject. This instead means initializing the object on the client. The client would initialize the proxy and the proxy would initialize the object only when required. This is especially useful when subject initialization does a great deal of configuration that the client will potentially never need.

Rules
The subject class and the proxy class should implement the same interface.

Benefits
Using proxies can optimize your application by loading only the necessary resources.
Using a proxy class can make the implementation cleaner and easier to maintain.

Example

<?php
abstract class ReadFileAbstract
{
protected $fileName;
protected $contents;
public function getFileName()
{
return $this->fileName;
}
public function setFileName($fileName)
{
$this->fileName = $fileName;
}
public function getContents()
{
return $this->contents;
}
}
class ReadFile extends ReadFileAbstract
{
const DOCUMENTS_PATH = "/var/www/html";
public function __construct($fileName)
{
$this->setFileName($fileName);
$this->contents = file_get_contents(self::DOCUMENTS_PATH . "/" . $this->fileName);
}
}
class ReadFileProxy extends ReadFileAbstract
{
private $file;
public function __construct($fileName)
{
$this->fileName = $fileName;
}
public function lazyLoad()
{
if ($this->file === null) {
$this->file = new ReadFile($this->fileName);
}
return $this->file;
}
}
$proxies = array();
for ($i = 0; $i < 10; $i++) {
// tell the proxy which file should be read (when will lazy loaded)
$proxies[$i] = new ReadFileProxy("sample" . $i . ".txt");
}
// Now it's time to read the contents of sample3.txt
$file3 = $proxies[3]->lazyLoad();
// echo the contents of sample3.txt
echo $file3->getContents();
?>

Proxy Pattern

Conclusion

As described above, one of the use cases of the proxy model is the slow loading of resources. In this case, it is the content of the file that loads slowly. Within the for loop 10 instances of the ReadFileProxy class are created, however, the contents of file 3 are only read on a call to the lazyLoad () method. This means that the other 9 proxy objects that were created did not need to perform slow read operations on the file system.

The English language is the most widely used language as a medium of communication around the world. Having a certification in the English language can be an advantage. StudySection provides an English Certification Exam that tests English language proficiency in English grammar, reading, and writing.

Leave a Reply

Your email address will not be published.