Home arrow PHP arrow Page 4 - Introducing the Chain of Responsibility Between PHP Objects

Completing the chain of responsibility - PHP

This article, the first of three parts, introduces you to the chain of responsibility pattern in PHP 5. This pattern is useful when a specific class cannot deal with a particular task, and must therefore transfer a program's execution to another class with the appropriate capability.

  1. Introducing the Chain of Responsibility Between PHP Objects
  2. The why and how of the chain of responsibility
  3. Creating a few sub classes
  4. Completing the chain of responsibility
  5. Making the chained classes work together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
October 30, 2006

print this article



In this case, since I wish to have at hand another sub class that eventually moves the program's execution up the chain of responsibility, I'll simply define a new child class, which will be tasked with saving plain strings to a given text file. Not surprisingly, the signature of this class is extremely simple and looks like this:

// define 'StringSaver' class
class StringSaver extends AbstractDataSaver{
    private $filePath;
    private $data;
    private $parentDataSaver;
    public function __construct($filePath,DataSaver
    // get path for data file
    public function getFilePath(){
        return $this->filePath;
    // get string of data
    public function getData(){
            // call parent object in chain of responsibility
            return $this->parentDataSaver->getData();
            return $this->data;
    // set string of data
    public function setData($data){
            throw new Exception('Data must be a string!');
    // save string to file
    public function saveData(){
            throw new Exception('Error opening data file!');
            throw new Exception('Error saving array to data

If you consider that the logic implemented by the previous "ArraySaver" sub class was really easy to understand, then you'll find the behavior followed by the above class even simpler.

Please notice how its "getData()" method also calls the corresponding parent class (inputted via the respective constructor) when it's not possible to handle the absence of the $this->data property. This is nearly identical to the approach followed by the other "ArraySaver" sub class, defined opportunely in the previous section.

All right, I strongly believe that the two subclasses that you learned before are more than enough to give you a correct understanding of how a specific chain of responsibility can be established between a group of PHP classes.

Considering this situation, in the final section of this article I'll show you how all these classes can interact with each other, implementing the chain of responsibility that I discussed right from the very beginning of this article.

Want to learn how this will be achieved? Then go ahead and click on the link below.

>>> 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: