PHP
  Home arrow PHP arrow Page 2 - Working with CSS Styles and the Stage Pattern in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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

Working with CSS Styles and the Stage Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-04-25


    Table of Contents:
  • Working with CSS Styles and the Stage Pattern in PHP 5
  • Building a target class
  • Defining a basic contextual class
  • Completing the implementation of the stage pattern

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Working with CSS Styles and the Stage Pattern in PHP 5 - Building a target class
    ( Page 2 of 4 )

    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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT