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.