PHP
  Home arrow PHP arrow Page 3 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 - Changing the behavior of a target class


    (Page 3 of 4 )

    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){
          $this->div=$div;
        }
        // get 'Div' object
        public function getDiv(){
          return $this->div;
        }
        // get (X)HTML markup of 'Div' object
        public function getDivHTML(){
          $this->divOverflow=strlen($this->div->getData())
    >64?'scroll':'hidden';
          return '<div id="'.$this->div->getId().'" class="'.$this-
    >div->getClass().'" style="overflow: '.$this-
    >divOverflow.';">'.$this->div->getData().'</div>';
        }
    }

    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


       · 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

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway