PHP
  Home arrow PHP arrow Page 2 - Introducing the Facade Pattern in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Introducing the Facade Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2007-01-16


    Table of Contents:
  • Introducing the Facade Pattern in PHP 5
  • A basic implementation of the facade pattern
  • Using HTTP compression with PHP 5
  • Completing the facade schema
  • Seeing the facade pattern in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Introducing the Facade Pattern in PHP 5 - A basic implementation of the facade pattern
    ( Page 2 of 5 )

    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
    Programming'){
         if(!$title){
           throw new Exception('A title for the web page must be
    supplied!');
         }
         $this->title=$title;
         if(!$keywords){
           $webPage->getXHTML();
         }
         $this->keywords=$keywords;
       }
       // 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-
    strict.dtd">'."n".'<html
    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-
    >title.'</title>'."n".'</head><body>'."n";
       }
       // create web page content section
       public function makeBody($content){
         if(!$content){
           throw new Exception('Content for the web page must be
    supplied!');
         }
         $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
    Section</h2><p>'.$content.'</p></div>'."n".'<div
    id="footer"><h2>Footer Section</h2></div>'."n";
       }
       // create web page footer section
       public function makeFooter(){
         $this->html.='</body>'.'</html>';
       }
       // 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
    try{
       $webPage=new WebPage('This is the sample title for the web
    page','keyword 1,keyword 2,keyword 3');
       $webPage->makeHeader();
       $webPage->makeBody('This is a sample content for the web
    page');
       $webPage->makeFooter();
       echo $webPage->getXHTML();
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT