HomePHP An Introduction to Building Proxy Classes with PHP 5
An Introduction to Building Proxy Classes with PHP 5
If you create object-oriented programs in in PHP 5, you know that their performance can be improved by rationalizing the use of objects. The proxy pattern can help with this task. In this first of a two-part series, you will learn the key points of how to use this pattern with PHP 5.
During your life as PHP developer, quite possibly you'll have learned some useful approaches to improve the overall performance of object-based web applications. From using content/XHTML caching mechanisms and compressing web page outputs, to implementing a rational usage of the objects utilized by a given application, options are numerous. Logically, the best of this variety of possible improvements rests on the point that many of the methods I mentioned can be used in conjunction with applications that have been written using a procedural methodology.
Nevertheless, when it comes to working solely with object-oriented programs in PHP, it's possible to create applications that present a carefully-planned utilization of their different objects. One of the simplest approaches, implemented to avoid the duplication of costly objects, involves using the popular references in PHP 4 instead of working with multiples copies of the objects in question.
However, if you're looking for new techniques aimed at improving the performance of object-based PHP applications as a whole, I must say that there's more good news on the horizon: in the huge terrain of creational design patterns, there's one that can be used successfully for implementing a rational usage of objects. In this case, I'm referring specially to the proxy pattern, which offers many interesting features that can help to reduce significantly the overhead of an application.
But I'm sure you're wondering how this pattern works. Well, in short, in the proxy pattern, one object is created by another only when the functionality of the first one is required. This avoids unnecessary class instantiation. For this pattern in particular, the object called "proxy" is responsible for handling all the accesses to another class, something that looks very similar to the behavior of different proxy servers. Now, are you feeling more familiar with the functionality of this useful pattern? I'm sure you are.
Having introduced the basic definition of proxy objects in PHP, in this two-part series, I'm going to walk you through the key points of how to use this pattern with PHP 5, as well as how to apply it in different real-world situations.
With the preliminaries out of our way, let's start learning more about this handy design pattern. Let's go!