PHP
  Home arrow PHP arrow Page 3 - Implementing the Stage Pattern in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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