PHP
  Home arrow PHP arrow Page 2 - 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 - 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


       · 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