{"id":6084,"date":"2022-09-14T04:30:08","date_gmt":"2022-09-14T04:30:08","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=6084"},"modified":"2022-09-14T06:00:06","modified_gmt":"2022-09-14T06:00:06","slug":"how-to-use-proxy-pattern-in-php","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/","title":{"rendered":"How to use Proxy Pattern in PHP"},"content":{"rendered":"<p>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).<\/p>\n<h2>When to use the proxy model<\/h2>\n<p>One of the main use cases of optimization for the proxy model in <a href=\"https:\/\/studysection.com\/blog\/explain-this-pattern-with-example-in-php-server-session-state\/\">PHP<\/a> 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.<\/p>\n<p><strong>Rules<\/strong><br \/>\nThe subject class and the proxy class should implement the same interface.<\/p>\n<p><strong>Benefits<\/strong><br \/>\nUsing proxies can optimize your application by loading only the necessary resources.<br \/>\nUsing a proxy class can make the implementation cleaner and easier to maintain.<\/p>\n<h3>Example<\/h3>\n<p><code>&lt;?php<br \/>\nabstract class ReadFileAbstract<br \/>\n{<br \/>\nprotected $fileName;<br \/>\nprotected $contents;<br \/>\npublic function getFileName()<br \/>\n{<br \/>\nreturn $this-&gt;fileName;<br \/>\n}<br \/>\npublic function setFileName($fileName)<br \/>\n{<br \/>\n$this-&gt;fileName = $fileName;<br \/>\n}<br \/>\npublic function getContents()<br \/>\n{<br \/>\nreturn $this-&gt;contents;<br \/>\n}<br \/>\n}<br \/>\nclass ReadFile extends ReadFileAbstract<br \/>\n{<br \/>\nconst DOCUMENTS_PATH = \"\/var\/www\/html\";<br \/>\npublic function __construct($fileName)<br \/>\n{<br \/>\n$this-&gt;setFileName($fileName);<br \/>\n$this-&gt;contents = file_get_contents(self::DOCUMENTS_PATH . \"\/\" . $this-&gt;fileName);<br \/>\n}<br \/>\n}<br \/>\nclass ReadFileProxy extends ReadFileAbstract<br \/>\n{<br \/>\nprivate $file;<br \/>\npublic function __construct($fileName)<br \/>\n{<br \/>\n$this-&gt;fileName = $fileName;<br \/>\n}<br \/>\npublic function lazyLoad()<br \/>\n{<br \/>\nif ($this-&gt;file === null) {<br \/>\n$this-&gt;file = new ReadFile($this-&gt;fileName);<br \/>\n}<br \/>\nreturn $this-&gt;file;<br \/>\n}<br \/>\n}<br \/>\n$proxies = array();<br \/>\nfor ($i = 0; $i &lt; 10; $i++) {<br \/>\n\/\/ tell the proxy which file should be read (when will lazy loaded)<br \/>\n$proxies[$i] = new ReadFileProxy(\"sample\" . $i . \".txt\");<br \/>\n}<br \/>\n\/\/ Now it's time to read the contents of sample3.txt<br \/>\n$file3 = $proxies[3]-&gt;lazyLoad();<br \/>\n\/\/ echo the contents of sample3.txt<br \/>\necho $file3-&gt;getContents();<br \/>\n?&gt;<br \/>\n<\/code><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/09\/Proxy-Pattern.png\" alt=\"Proxy Pattern\" \/><\/p>\n<h3>Conclusion<\/h3>\n<p>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.<\/p>\n<p><small><em>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 <a href=\"https:\/\/www.studysection.com\/english-language-advanced-diploma\">English Certification Exam<\/a> that tests English language proficiency in English grammar, reading, and writing.<\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The proxy model is a way to create an intermediary between your client\/controller and your disciplinary class. Think of an<\/p>\n","protected":false},"author":1,"featured_media":6085,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[200,803],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use the Proxy Pattern in PHP - StudySection Blog<\/title>\n<meta name=\"description\" content=\"The proxy pattern model is a way to create an intermediary between your client or controller and your disciplinary class.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use the Proxy Pattern in PHP - StudySection Blog\" \/>\n<meta property=\"og:description\" content=\"The proxy pattern model is a way to create an intermediary between your client or controller and your disciplinary class.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-14T04:30:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-14T06:00:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/09\/Proxy-Pattern-in-PHP.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"How to use Proxy Pattern in PHP\",\"datePublished\":\"2022-09-14T04:30:08+00:00\",\"dateModified\":\"2022-09-14T06:00:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/\"},\"wordCount\":321,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"php\",\"Proxy Pattern\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/\",\"url\":\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/\",\"name\":\"How to use the Proxy Pattern in PHP - StudySection Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-09-14T04:30:08+00:00\",\"dateModified\":\"2022-09-14T06:00:06+00:00\",\"description\":\"The proxy pattern model is a way to create an intermediary between your client or controller and your disciplinary class.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use Proxy Pattern in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use the Proxy Pattern in PHP - StudySection Blog","description":"The proxy pattern model is a way to create an intermediary between your client or controller and your disciplinary class.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/","og_locale":"en_US","og_type":"article","og_title":"How to use the Proxy Pattern in PHP - StudySection Blog","og_description":"The proxy pattern model is a way to create an intermediary between your client or controller and your disciplinary class.","og_url":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-09-14T04:30:08+00:00","article_modified_time":"2022-09-14T06:00:06+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/09\/Proxy-Pattern-in-PHP.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"How to use Proxy Pattern in PHP","datePublished":"2022-09-14T04:30:08+00:00","dateModified":"2022-09-14T06:00:06+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/"},"wordCount":321,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["php","Proxy Pattern"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/","url":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/","name":"How to use the Proxy Pattern in PHP - StudySection Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-09-14T04:30:08+00:00","dateModified":"2022-09-14T06:00:06+00:00","description":"The proxy pattern model is a way to create an intermediary between your client or controller and your disciplinary class.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/how-to-use-proxy-pattern-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use Proxy Pattern in PHP"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":680,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6084"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=6084"}],"version-history":[{"count":6,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6084\/revisions"}],"predecessor-version":[{"id":6092,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6084\/revisions\/6092"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/6085"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=6084"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=6084"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=6084"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}