PHP
  Home arrow PHP arrow Page 2 - Introducing the Facade Pattern in PHP ...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 6
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · In this first part of the series, the foundations of the facade pattern are...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT