PHP
  Home arrow PHP arrow Page 2 - 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 - Creating a base class
    ( Page 2 of 4 )



    As usual, a good point to start explaining how the decorator pattern works with PHP is simply by coding an easy-to-grasp example, thus you can understand from the very beginning the way this pattern can be applied inside a Web application.

    Considering the concepts that I deployed before, first I’ll code a sample PHP class, which will be used as a base structure. Then I'll create a decorator class which will extend the functionality of the base class. Therefore, here is the definition of the core class, which I called “StringSaver.” Please take a look at its source code:

    // define 'StringSaver' class (this base class will be decorated
    later on)
    class StringSaver{
        private $path;
        private $str;
        public function __construct($path,$str){
            $this->path=$path;
            $this->str=$str;
        }
        public function save(){
            if(!$fp=fopen($this->path,'w')){
                throw new Exception('Error opening string file');
            }
            fwrite($fp,$str);
            flose($fp);
        }
        public function getPath(){
            return $this->path;
        }
        public function getString(){
            return $this->str;
        }
    }

    Even when the above class looks rather primitive, its functionality is indeed easy to understand. As you can see, this class performs a simple task, which merely consists of saving an input string to a given text file, previously passed as a parameter to the constructor.

    Also, aside from exposing the respective “save()” method, the class has two additional ones. These are “getPath()” and “getString()” respectively, and they are responsible for returning the two properties previously assigned inside the constructor, that is $path and $str.

    So far, as you’ll agree with me, the previous class certainly can’t be considered anything special. However, suppose that you want to extend its functionality without modifying its original structure, and also without creating a sub class from it. How can this be done? Well, here is where the decorator pattern comes in, since it’s possible to define a decorator class which can extend the operability of the base class, meeting all the requirements that I mentioned before.

    Therefore, in the next section, I’ll show you how to create a decorator class that can dd more functionality to the previous “StringSaver” class. Thus, if you wish to learn how this will be achieved, go ahead and keep reading.



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek