PHP
  Home arrow PHP arrow Page 4 - Implementing the Stage Pattern in PHP ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
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: 3 stars3 stars3 stars3 stars3 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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 - Seeing the stage pattern in action


    (Page 4 of 4 )

    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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · If you’re interested on creating a class that changes its behavior in response to...
       · The title of this article uses "Stage Pattern", however in the article body it...
       · Thank you for pointing me out that error in the article. It was my mistake, and you...
       · Thank you for pointing out that error to me. Actually, the correct name of the...
       · HI, Is there a way in which this can be done using php4? I don't know all the...
       · Thank you for commenting on my PHP article. Now with referene to your question, of...
     

       

    PHP ARTICLES

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT