PHP
  Home arrow PHP arrow Page 3 - An Introduction to Using the Decorator...
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 
Mobile Linux 
App Generation ROI 
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

An Introduction to Using the Decorator Pattern with PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 12
    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:
      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


    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


       · Over the course of this first article, you'll learn how to apply the decorator...
       · Very good tutorial. Looking forward to the next in series.
       · Thank you for the kind words on my PHP article. Also, I'm glad to know you liked it,...
       · hihow does the strupperDecorator object modify the strDecorator object's str...
       · Hi Dave,With reference to your question, StringDecorator and...
       · it's better to check return of fwrite as well, not only fopen ;)
       · Thank you for the feedback. Yeap, you're correct, but I just wanted to write down...
       · flose($fp); by fclose($fp);fwrite($fp,$str); by fwrite($fp,$this->str);thx...
       · Thank you for the useful contributions to my PHP article. I think they'll be...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





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