Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Working with CSS Styles and the Stage Pattern in PHP 5
  2. Building a target class
  3. Defining a basic contextual class
  4. Completing the implementation of the stage pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 25, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: