HomePHP Page 3 - Working with CSS Styles and the Stage Pattern in PHP 5
Defining a basic contextual class - PHP
The stage pattern lets you build classes that can modify their behaviors according to the variations of a given programming context. If you want to learn more about it, then you should start reading this article now! Welcome to the final installment of the series that began with "Implementing the stage pattern in PHP 5." Made up of two articles, this series walks you through the application of this useful pattern, and complements the corresponding theory with illustrative code samples.
As I expressed in the section you just read, to complete the programmatic model imposed by the stage pattern, it's necessary to build a contextual class capable of selecting the appropriate CSS style that fits the requirements of a specific web document.
To perform this simple task, below I defined the brand new "CSSContext" class. It is responsible for returning the correct CSS object to client code in response to the format demands of a given web page.
Now that I have explained how this contextual class is going to work, please pay attention to its respective signature:
// define 'CSSContext' class class CSSContext{ private $pageFormat=NULL; public function __construct(){} public function setPageFormat($pageFormat){ if($pageFormat!='normal'&&$pageFormat!='print'){ throw new Exception('Invalid value for web page format.'); } $this->pageFormat=$pageFormat; } public function getCSS(){ if($this->pageFormat=='normal'){ return new CSSNormal('Verdana','#f00','#eee'); } else{ return new CSSPrint('Arial','#000','#fff'); } } }
As I said before, the above class has been provided with the capacity to create a specific CSS object in accordance with the format requirements of a particular web document. As you can see, this task is performed by its "getCSS()" method, which returns to calling code either a CSS object suitable for use with computer monitors, or another one for use with printers.
All right, at this stage I have defined a contextual class that controls what type of CSS object must be used according to the specifications of a given web document. However, there's a missing piece in this scenario, since I need to create a mechanism that generates the web document in question.
In response to this requirement, below I included the definition of a brand new class. It's called "WebPage," and it is tasked with creating web pages on the fly, using a specific CSS object passed as an input parameter to the corresponding constructor.
The signature for the aforementioned class is as follows:
// define 'WebPage' class class WebPage{ private $title='Testing State pattern'; private $html=NULL; private $css=NULL; public function __construct(CSS $css){ $this->css=$css; } public function makeHeader(){ $this->html='<html><head><title>'.$this- >title.'</title></head>'; } public function makeBody(){ $this->html.='<body><div style="font: bold 12px '.$this- >css->getFont().';color :'.$this->css->getColor().';background: '.$this->css->getBackground().';">This is the default content used to test the state pattern.</div>'; } public function makeFooter(){ $this->html.='</body></html>'; } public function getHTML(){ return $this->html; } }
Now that you have seen the definition for the above "WebPage" class, I'm pretty certain that you'll see more clearly how the stage pattern is implemented in this case. Let me explain the situation briefly: on one hand, there's a contextual class that selects the proper CSS object to be used when generating a web document, while on the other hand there's another class that changes its behavior -- notice the implementation of the "makeBody()" method -- to fit the requirements of a particular page format.
Okay, at this point you hopefully understand how the stage pattern is used here. However, I think that you'll grasp the way that this pattern works even more easily if I set up a concrete example where all the previous classes are put to work in conjunction.
In the following section I'm going to develop a short script that will help to demonstrate the functionality of the stage pattern. Keep reading, please.