HomePHP Page 2 - Working with CSS Styles and the Stage Pattern in PHP 5
Building a target 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 said in the beginning of this article, my demonstration of how to implement the stage pattern with PHP 5 will consist mainly of creating a simple mechanism that generates dynamically two different versions of a given web document. The first one will be best suited for use with conventional computer monitors, and the second one will be simply a "printer-friendly" format of the document in question.
Of course, as you might have guessed, selecting the proper CSS styles that fit a particular web page format will be a task performed by two concrete classes, in this case called "CSSNormal" and "CSSPrint" respectively.
The signatures corresponding to these two concrete classes are shown below, including the pertinent definition of their parent. Having said that, the classes look like this:
// define abstract 'CSS' class abstract class CSS{ protected $font; protected $color; protected $background; abstract public function __construct ($font,$color,$background); abstract public function getFont(); abstract public function getColor(); abstract public function getBackground(); }
// define concrete 'CSSNormal' class class CSSNormal extends CSS{ public function __construct($font,$color,$background){ if($font!='Arial'&&$font!='Tahoma'&&$font!='Verdana'){ throw new Exception('Invalid value for font property.'); } if(!preg_match("/^#[0-9a-f][0-9a-f][0-9a-f]$/",$color)){ throw new Exception('Invalid value for foreground color property.'); } if(!preg_match("/^#[0-9a-f][0-9a-f][0-9a-f]$/",$background)){ throw new Exception('Invalid value for background color property.'); } $this->font=$font; $this->color=$color; $this->background=$background; } public function getFont(){ return $this->font; } public function getColor(){ return $this->color; } public function getBackground(){ return $this->background; } }
// define concrete 'CSSPrint' class class CSSPrint extends CSS{ public function __construct($font,$color,$background){ if($font!='Arial'){ throw new Exception('Invalid value for font property.'); } if($color!='#000'){ throw new Exception('Invalid value for foreground color property.'); } if($background!='#fff'){ throw new Exception('Invalid value for background color property.'); } $this->font=$font; $this->color=$color; $this->background=$background; } public function getFont(){ return $this->font; } public function getColor(){ return $this->color; } public function getBackground(){ return $this->background; } }
As you can see, the concrete classes listed above are very easy to grasp. Basically, their primary task is to create a CSS style that suits the requirements of a particular web page format.
In the first case, the "CSSNormal" class is tasked with building a CSS style that's best suited for use with computer screens, while the second class, called "CSSPrint," is obviously responsible for creating a set of styles for use with printers.
So far, so good. At this point I have built two simple classes that are capable of generating diverse CSS styles for applying to a specific web document. However, at this moment I'm sure you're asking the following question: how does the stage pattern fit into this schema? I'm glad you asked!
Having at our disposal the two classes previously defined, I'm going to create a contextual class. It will be able to change its behavior in order to select a specific CSS style according to the format requirements of a given web page. In doing so, I'll be implementing the model demanded by the stage pattern.
Naturally, this brand new contextual class will be defined in the following section, therefore click on the link that appears below and keep reading.