What is a proxy class?
A proxy class is simply a class that is used in place of another class, almost always for performance reasons. This class sits in front of the other class, and doesn’t instantiate until a function within the original class is called. Proxies instead contain logic for delaying the instantiation process of the original class. So rather than instantiating the class right away, it is only instantiated when a method on that class is called.
The term “proxy” is used in other areas of programming & computing as well. For example, when someone visits yourdomain.com, it’s generally served up by one specific application running on a single web server. But in the past, I’ve created web server proxies. These can basically funnel specific requests to other services in the background. This means that you can create a proxy just for a specific URL of your domain, so that it is served up by an entirely separate application & service.
In this example, we can set up a web server such as Nginx to listen for a specific URL, let’s say slash-blog, and that request can be served up by an entirely separate app, running on a completely different stack.
This is how you would be able to setup Magento as your default service on your main web root, but also have something like Nuxt.js running a Node.js backend for your slash-blog route, or Wordpress running a completely different version of PHP & MySQL on an entirely different web server. The directory structure would not at all need to be located in the same location as your Magento instance, because it’s an entirely different app, running a completely different web stack on a completely separate server!
You can think of proxy classes as being very similar. When a specific class is requested, another class can be served up, and no one else really even needs to know about it. The delaying of the instantiation process of the original class basically makes the class load instantly, so your app can continue to execute without having performance issues.
Lesson Summary
A Proxy class is a class utilized in place of another class, primarily for performance optimization. It delays instantiation until a method within the original class is called, thereby enhancing efficiency in various programming and computing scenarios. Some applications of Proxy classes are:
- Web server proxies route specific requests to different services for improved performance.
- Proxy classes enable deferred functionalities until needed, thereby enhancing overall performance.
The use of web server proxies is exemplified by the ability to route specific requests from a domain to various applications and services on different servers:
- Web servers like Nginx can listen for specific URLs.
- Requests are routed to different applications without performance degradation.
- Multiple applications, such as Magento, Nuxt.js, and Wordpress, running on separate servers can serve distinct routes under a single domain.
This setup provides flexibility in utilizing different technologies for various sections of a website, while still ensuring efficient performance is maintained.