HomePHP Page 3 - Introducing the Facade Pattern in PHP 5
Using HTTP compression with PHP 5 - PHP
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.
In consonance with the concepts that I deployed in the section that you just read, the next step that I’m going to take will consist of defining a basic facade class. This class will be capable of applying HTTP compression to any input string, in addition to removing any eventual new line characters from it.
Having outlined the functionality of this simple facade class, please pay attention to its definition. It’s as follows:
// define 'CompressContentFacade' class class CompressContentFacade{ public static function stripContent($content){ // remove new lines from content $strippedContent=StripContent::stripString($content); // compress content by using GZIP algorithm $gzippedContent=GzipContent::compressString ($strippedContent); return $gzippedContent; } }
Despite the remarkable simplicity of the above facade class, there are a few things worth stressing about its definition. First, notice how the class in question hides all the complexity required for removing new lines characters and compressing strings behind the interface of two different classes, “StripContent” and “GzipContent” respectively.
Second, you should realize that all of these classes are completely independent from the facade one, which means that they know nothing about the class that called them. If you recall the definition of the facade pattern, this is exactly the schema required for applying it. Now, the prior facade class should make a lot of sense to you!
Nevertheless, I must mention that the previous “CompressContentFacade” class is indeed only half of the story when it comes to implementing the facade pattern with PHP 5, since the corresponding string compressing classes that you saw before remain undefined.
To address this issue, in the following section I’m going to show you the respective signature for these two new classes, in this way completing the model dictated by the facade pattern.
Do you wish to see how these classes will be created? Jump ahead and read the next few lines.