PHP
  Home arrow PHP arrow Page 3 - 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 - 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


       · 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 1 hosted by Hostway
    Stay green...Green IT