HomePHP 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.
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:
try{ // create new 'Div' object $div=new Div(); // set Div properties $div->setData('This is a sample string that will be included into the DIV.'); $div->setId('newid'); $div->setClass('newclass'); // create new 'DivContext' object $divContext=new DivContext($div); // display DIV element (its overflow property has a value of 'hidden') 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 characters.'); // display DIV element (its overflow property has a value of 'scroll') echo $divContext->getDivHTML(); } catch(Exception $e){ echo $e->getMessage(); exit(); }
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!