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

Seeing the stage pattern in action - 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



As you certainly recall from the previous section, I thought that the best way to understand the functionality provided by the stage pattern is by simply setting up a concrete example where the pair of classes that you learned earlier are put to work in tandem.

Since I consider this a good approach to demonstrate how the pattern in question works, in the next few lines I coded a short script, which shows how a contextual div object is capable of modifying the behavior of a given DIV element, in this case by changing the value of its “overflow” CSS property.

As I explained in the prior section, the aforementioned CSS property will be modified according to the length of the contents included into the target DIV, certainly a condition that is in evidence by the below example.

That being said, please take a look at the following code listing:

   // create new 'Div' object
   $div=new Div();
   // set Div properties
   $div->setData('This is a sample string that will be included
into the DIV.');
   // create new 'DivContext' object
   $divContext=new DivContext($div);
   // display DIV element (its overflow property has a value of
   echo $divContext->getDivHTML();
   // set new data for Div element
   $div->setData('This is a sample string that will be included
into the DIV, but in this case the behavior of the DIV object
will change since the length of this data is larger than 64
   // display DIV element (its overflow property has a value of
   echo $divContext->getDivHTML();
catch(Exception $e){
   echo $e->getMessage();

As demonstrated above, the contextual div object is capable of modifying the value of the “overflow” CSS property that belongs to the target DIV, in this way changing its behavior according to the requirements of a given context.

Now that you grasped the logic that stand behinds the stage pattern, I encourage you to develop your own examples, so you can acquire a more solid background on how this pattern can be used in concrete situations.

Final thoughts

Sadly, we’ve come to the end of this first article of the series. As you saw previously, the stage pattern can be useful in those cases where the context of a particular PHP application is going to change very frequently.

However, if you believe that all the hands-on examples shown in this tutorial are not enough material, I’ve got good news to you. In the last part of the series I’m going to show you how to implement the stage pattern to generate “screen” and printer-friendly versions for a bunch of web documents.

Now that you’ve been warned, are you going to miss it? I hope not!

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