PHP
  Home arrow PHP arrow Page 3 - An Introduction to Using the Decorator Pattern with PHP
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? 
Google.com  
PHP

An Introduction to Using the Decorator Pattern with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 15
    2006-08-28


    Table of Contents:
  • An Introduction to Using the Decorator Pattern with PHP
  • Creating a base class
  • Defining the structure of a Decorator object
  • Creating additional decorator classes

  • 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


    An Introduction to Using the Decorator Pattern with PHP - Defining the structure of a Decorator object
    ( Page 3 of 4 )

    As you learned in the previous section, once the base “StringSaver” class has been defined, creating a decorator class is indeed a straightforward task to perform. Now, with reference to extending the existing functionality of the “StringSaver” class, suppose that you want to manipulate its $str property, in many creative and clever ways (I’m just kidding), such as uppercasing and lowercasing it. Sounds pretty cool, right?

    Okay, in order to do that, I’m going to define a decorator class which will take the $str property of the base class and assign it as its own property. In this way it will act like a programmatic bridge for further decorators.

    As usual, theory should be supported by the corresponding hands-on examples, so below is the definition of the “StringDecorator” class. Please examine its signature:

    class StringDecorator{
        protected $strsaver;
        public $str;
        // pass 'StringSaver' object to constructor
        public function __construct(StringSaver $strsaver){
            $this->strsaver=$strsaver;
            // retrieve string from original 'StringSaver' object
            $this->resetString();
        }
        // obtain string from 'StringSaver' object
        // thus the original property remains the same
        public function resetString(){
            $this->str=$this->strsaver->getString();
        }
        // return string to calling code
        public function displayString(){
            return $this->str;
        }
    }

    If you take a look at the above class, then you’ll realize that there are some interesting things worth noting. First, the “StringDecorator” class accepts an object of type “StringSaver” as its unique parameter, which is assigned as a class property inside the constructor.

    However, the most relevant thing happens in relation with the “resetString()” method, since it uses the “getString()” method that belongs to the “StringSaver” class and obtains its $str property. While this is a simple concept, it demonstrates how to build a class that takes up the properties of a base class without modifying its original structure. Now, do you see how a decorator class fits into the whole picture?

    All right, at this point, the “StringDecorator” class is limited to taking the $str property from the base class, but aside from that, it’s not doing anything else. Therefore, it’s time to leap forward and see how the original $str property can be uppercased and lowercased by deriving a couple of concrete subclasses from the “StringDecorator” class.

    That’s exactly the subject of the next section, so I recommend you jump straight into it and keep learning more.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek