The facade pattern may be one of the less well known patterns in PHP, but it definitely has its uses. In this first article of a two-part series, you'll learn the basics of how the facade pattern works, illustrated with numerous examples.
As youíll certainly agree, pattern-based PHP programming is sometimes a hard-to-master topic. It logically has to be addressed with a considerable level of knowledge (and experience, to be frank), more specifically in those cases where the design pattern to be implemented presents a high grade of abstraction.
Of course, this doesnít mean that all of the PHP applications that youíre going to develop from this point onward must solve programming-related problems by utilizing a particular pattern. However, learning how to apply different design patterns to tackle common and well-known issues may help to expand your existing background as PHP developer, in addition to improving your overall experience.
Assuming that the previous paragraphs already convinced you that learning pattern-based programming with PHP might be an instructive and educational experience, in this series, which is comprised of two comprehensive articles, Iím going to introduce the foundations of an interesting structural pattern. Iím talking specifically of the facade pattern.
Since I have to admit that this one isnít very commonly used amid the huge terrain of PHP-based web development, let me first walk you through a brief introduction about what it does. In this way you can anticipate what youíll learn in the course of these two articles.
To put it simply, the facade pattern can be applied in those situations where you need to create a PHP class which requires the implementation of a complex system as part of one of its methods. At first glance, the previous definition sounds like something applicable in many cases, but the advantage of using a facade schema relies on the point that the class can hide all the logic of the mentioned mechanism, while the mechanism in question knows nothing about the calling class. That sounds pretty good, right?
To clarify the above concepts a bit more, allow me to cite an illustrative example: suppose you have a class which uses a complex set of additional classes to serialize/unserialize different objects before saving them to a database table. In this case, you have a class that hides all the complexity required for serializing the mentioned objects, but thereís also a group of classes that know nothing about the class that called them. As you can see, these classes are only responsible for performing the serialization/unserialization sequence on several objects and nothing else. Period.
Now, are youíre starting to grasp the logic behind the facade pattern? Of course you are!
At this moment , and after explaining a few basic points on how to use the facade pattern with PHP, letís move forward and learn how all these theoretical concepts can be translated into some functional hands-on examples. Are you ready? Good, letís get started!