PHP
  Home arrow PHP arrow Page 2 - Implementing the Stage Pattern in PHP ...
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

Implementing the Stage Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2007-04-18

    Table of Contents:
  • Implementing the Stage Pattern in PHP 5
  • Creating the programmatic model of the state pattern
  • Changing the behavior of a target class
  • Seeing the stage pattern in action

  • 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


    Implementing the Stage Pattern in PHP 5 - Creating the programmatic model of the state pattern


    (Page 2 of 4 )

    A good place to start demonstrating the functionality provided by the stage pattern is with defining what I call a “target” class. As you’ll see for yourself in a moment, this class will be capable of modifying its behavior according to the variations introduced into its working environment.

    Basically, the previously mentioned target class will be tasked with creating simple DIV elements, which will have their “overflow” CSS property set to a value of “hidden” by default. However, if it happens that the context where these DIVs will be used eventually changes, and in addition, the contents wrapped by them grows in size, then the DIVs will modify their overflow property to “scroll.”

    Undoubtedly, this is a good example of a class that modifies its behavior in response to changes occurring inside its context; this precisely reflects the schema dictated by the stage pattern.

    Okay, now that I have explained how I plan to illustrate the functionality of the stage pattern, please examine the signature of the “Div” class below, which as I stated previously, is responsible for displaying regular DIVs.

    The definition for this brand new class is as follows: 

    // define 'Div' class
    class Div{
       private $data='default_data';
       private $id='divid';
       private $class='divclass';
       public function __construct(){}
       // set Div data
       public function setData($data){
         if(!preg_match("/[a-zA-Z0-9]+/",$data)){
           throw new Exception('Invalid DIV data has been
    supplied.');
         }
         $this->data=$data;
       }
       // set Div Id
       public function setId($id){
         if(!preg_match("/^[a-z]+$/",$id)){
           throw new Exception('Invalid DIV id has been supplied.');
         }
         $this->id=$id;
       }
       // set Div class
       public function setClass($class){
         if(!preg_match("/^[a-z]+$/",$class)){
           throw new Exception('Invalid DIV class has been
    supplied.');
         }
         $this->class=$class;
       }
       // get Div data
       public function getData(){
         return $this->data;
       }
       // get Div id
       public function getId(){
         return $this->id;
       }
       // get Div class
       public function getClass(){
         return $this->class;
       }
    }

    As you can see, the structure corresponding to the above “Div” class is indeed very easy to follow. The class in question presents the typical modifiers and accessing methods, which come in handy for setting and retrieving the values assigned to its “$data,” $id” and “$class” properties respectively, so I guess you shouldn’t have major problems understanding how it works.

    However, as I explained earlier, the previous “Div” class is only a part of the stage pattern, since it can’t change its behavior by itself when the context varies. Therefore it’s necessary to define another class, which will be responsible for controlling the context where the target class is utilized, and eventually change its behavior.

    Taking into account that a brand new contextual class has to be defined to complete the programmatic model imposed by the stage pattern, please jump into the following section and keep reading to learn how this class will be created.

    More PHP Articles
    More By Alejandro Gervasio


       · If you’re interested on creating a class that changes its behavior in response to...
       · The title of this article uses "Stage Pattern", however in the article body it...
       · Thank you for pointing me out that error in the article. It was my mistake, and you...
       · Thank you for pointing out that error to me. Actually, the correct name of the...
       · HI, Is there a way in which this can be done using php4? I don't know all the...
       · Thank you for commenting on my PHP article. Now with referene to your question, of...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - 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
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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