PHP
  Home arrow PHP arrow 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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


    (Page 1 of 4 )

    A decorator class allows you to add more capacity to an existing class while leaving the original class untouched. It has certain advantages over inheritance, as you will learn in this first article of a three-part series.

    Introduction

    Among the plethoric variety of design patterns that can be applied in software engineering, there are a few that are specifically interesting for web developers. These feature potentially easy implementation and versatility, particularly when working with object-oriented applications. Speaking more specifically, the Decorator pattern is one that I found myself using quite frequently inside my PHP-driven applications, in cases where I needed to construct a "bridging" class that was capable of extending the operability and functionality of a base class, but without having to introduce changes in its original structure.

    Does that sound a little bit complex? Well, fear not; let me point you in the right direction. A "decorator" class is one that allows you to add more capacity to an existing class, by using some intermediate methods, while leaving the original class untouched. Okay, you have every right to say that the same result can be achieved by using inheritance, and then creating some cool subclasses. However, in that case you'd be working with objects of the same type, while a "decorator" object belongs to a completely different family. Are you getting my point now? I bet you are.

    Indeed, one of the most appealing aspects of the decorator design pattern is the ease with which you can use a class of different type to extend the capabilities of another. Of course, this isn't always the best course of action to take, simply because it can be much easier to modify the original class by using inheritance, but there are situations where a decorator class can find its place inside an application.

    Due to the wide range of web applications where the decorator pattern can be successfully applied, over the course of this series, I'll show you some useful pointers to demonstrate how this pattern works, and how it can be introduced in your own PHP applications with only minor hassles. Before you start reading the rest of this article, make sure that you have a pretty good understanding of object-based programming with PHP, since you'll see an abundance of material related to this topic.

    Having introduced the basics of the decorator pattern, let's learn how it can be applied to PHP with several practical examples. Let’s get started!

    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

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway