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.
| | Discuss An Introduction to Using the Decorator Pattern with PHP | | | | | | | 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,... | | | | | | hi
how 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... | | | | | | >>> Post your comment now! | | | | | |
|