Home arrow PHP arrow Page 2 - Implementing the Stage Pattern in PHP 5

Creating the programmatic model of the state pattern - PHP

Updating modules of a web application can be quite a chore, especially when classes are involved. Wouldn't it be easier if there were a class that could update itself depending on its context? Fortunately there is. It's called the Stage pattern. This is the first part of a two-part series that introduces you to that pattern and its uses.

  1. Implementing the Stage Pattern in PHP 5
  2. Creating the programmatic model of the state pattern
  3. Changing the behavior of a target class
  4. Seeing the stage pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
April 18, 2007

print this article



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){
       throw new Exception('Invalid DIV data has been
   // set Div Id
   public function setId($id){
       throw new Exception('Invalid DIV id has been supplied.');
   // set Div class
   public function setClass($class){
       throw new Exception('Invalid DIV class has been
   // 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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: