Home arrow PHP arrow Page 2 - An Introduction to Using the Decorator Pattern with PHP

Creating a base class - PHP

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.

  1. An Introduction to Using the Decorator Pattern with PHP
  2. Creating a base class
  3. Defining the structure of a Decorator object
  4. Creating additional decorator classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 16
August 28, 2006

print this article



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){
    public function save(){
            throw new Exception('Error opening string file');
    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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: