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

Changing the behavior of a target class - 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



In consonance with the concepts that I deployed in the section that you just read, a brand new class must be built to complete the model dictated by the stage pattern. As you’ll see shortly, this contextual class will be capable of modifying the behavior of the “Div” class that you saw earlier.

But how will this be done? In plain words, if the contents wrapped by a specific DIV are larger than a predefined length, the DIV will switch its “overflow” CSS property to a value of “scroll.” Otherwise, the property in question will keep its original value, which is “hidden.” Sounds simple, right?

Now that you know how this contextual class is going to work, study its corresponding signature. It is as follows:

// define 'DivContext' class
class DivContext{
    private $div=NULL;
    private $divOverflow=NULL;
    public function __construct(Div $div){
    // get 'Div' object
    public function getDiv(){
      return $this->div;
    // get (X)HTML markup of 'Div' object
    public function getDivHTML(){
      return '<div id="'.$this->div->getId().'" class="'.$this-
>div->getClass().'" style="overflow: '.$this-

After examining the definition of the above “DivContext” class, you’ll have to agree with me that its functionality is indeed remarkable, regardless of its short signature. As you can see, this brand new contextual class takes up a “Div” object as its unique input parameter and assigns it as a class property.

However, I’d like you to pay attention to the signature of the “getDivHTML()” method, since this method is actually the workhorse of the class. Please notice how the method determines what value for the “overflow” CSS property will be assigned to the target DIV, depending on the length of the contents that will be housed by this containing element.

In this case, there’s a class that will change the way it displays a DIV element on a web page, according to the changes introduced into its context, in this way implementing the so-called stage pattern. Isn’t that interesting?

Okay, at this stage you hopefully grasped the logic that drives the stage pattern, therefore I think it’s an excellent time to move forward and see how the pair of classes previously defined can be put to work in a fully-functional example.

As you might have guessed, this instructive example will be developed in the following section, thus click on the link below and keep reading.

>>> 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: