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

Implementing the Stage Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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
     

       

    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