PHP
  Home arrow PHP arrow Page 3 - 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? 
Google.com  
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 - Defining a basic contextual class
    ( Page 3 of 4 )

    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.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek