Home arrow PHP arrow Page 2 - Introducing the Facade Pattern in PHP 5

A basic implementation of the facade pattern - 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.

  1. Introducing the Facade Pattern in PHP 5
  2. A basic implementation of the facade pattern
  3. Using HTTP compression with PHP 5
  4. Completing the facade schema
  5. Seeing the facade pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
January 16, 2007

print this article



To demonstrate a simple implementation of the facade pattern with PHP 5, what I’ll do first will consist of defining a web page generator class. Later, the class will be capable of hiding all the complexity required for compressing web documents through a set of additional classes, which logically will be completely decoupled from the generator in question. Sounds really simple, doesn’t it?

Now that I've explained how I plan to implement the facade pattern with PHP, take a look at the signature of the class below. I named it “WebPage.” Not surprisingly, it is tasked with creating simple (yet dynamic) web documents. Its definition is as follows: 

// define 'WebPage' class
class WebPage{
   private $html;
   private $title;
   private $keywords;
   public function __construct($title='PHP Web
Page',$keywords='PHP,Web Development,Object-oriented PHP
       throw new Exception('A title for the web page must be
   // create web page header section
   public function makeHeader(){
     $this->html='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-
xmlns="http://www.w3.org/1999/xhtml">'."n".'<head><meta http-
equiv="Content-Type" content="text/html; charset=iso-8859-
1" />'."n".'<meta http-equiv="keywords" content="'.$this-
>keywords.'" />'."n".'<title>'.$this-
   // create web page content section
   public function makeBody($content){
       throw new Exception('Content for the web page must be
     $this->html.='<div id="header"><h2>Header
Section</h2></div>'."n".'<div id="navbar"><h2>Navigation Bar
Section</h2></div>'."n".'<div id="mainsection"><h2>Main
id="footer"><h2>Footer Section</h2></div>'."n";
   // create web page footer section
   public function makeFooter(){
   // get (X)HTML markup
   public function getXHTML(){
     return $this->html;

As you can see, the above web page generator class presents a typical structure for generating the different sections of a web document, that is its header section, then its main area, and finally the corresponding footer.

So far, the logic implemented by the previous class shouldn’t be hard for you to grasp at all. Therefore let me show you a short script which uses the prior class to create a hypothetical web document. The code for this example in particular is as follows:

// example using 'WebPage' class
   $webPage=new WebPage('This is the sample title for the web
page','keyword 1,keyword 2,keyword 3');
   $webPage->makeBody('This is a sample content for the web
   echo $webPage->getXHTML();
catch(Exception $e){
   echo $e->getMessage();

As you may have guessed, using the previously-defined web page generator class is indeed a straightforward process which only requires a minor effort from us. In simple terms, the above script builds a basic web document by calling in turn the “makeHeader(),” “makeBody()” and “makeFooter()” methods, and finally displays it on the browser via “getXHTML().”

At this point, I’m quite sure that you haven't had major problems understanding how the previous web page generator class works. However, you may be wondering how the facade pattern fit into this schema. Well, let me show you how to create a simple facade class which will be capable of compressing all sorts of strings, including the source code generated by the web page class that you just learned.

Naturally, the signature for this brand new facade class will be shown in the section to come, thus click on the link below and keep reading.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: