PHP
  Home arrow PHP arrow Page 2 - Working with CSS Styles and the Stage ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · This first article of the series walks through the key concepts on implementing the...
       · Where's the advantage? How is this any better than a simple if condition that...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT