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

The why and how of 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Naturally, it's good to learn how a predefined chain of responsibility can be created across several PHP classes by setting up a comprehensive example. This will help you to grasp the basic concepts of this theme.

To begin with, I'll define a group of simple classes, tasked with saving different types of data to a specified text file. Each class will be capable of delegating the execution of a particular process to its nearest parent when the current class is incapable of handling the process in question. That being said, here is the definition of the base "AbstractDataSaver" class, which will set up the blueprints for all of its respective sub classes:

// define abstract 'AbstractDataSaver' class
abstract class AbstractDataSaver{
    abstract public function getFilePath();
    abstract public function setData($data);
    abstract public function getData();
    abstract public function saveData();

}

In this case, the skeleton of the above abstract class exposes the core methods that will be concretely implemented by the respective children. Here, it's clear to see that each of these abstract methods conform to a generic model, which is handy for defining (in a later section) the structure of a few concrete classes that will be capable of storing distinct data on plain text files.

With reference to the signature of the prior "AbstractDataSaver" class, you can see that it first defines a "getFilePath()" method, which will eventually return the path of the text file to where the data will be saved. Next, it defines a set of additional methods, called "setData()", "getData()" and "saveData()" respectively, tasked with accessing and modifying the data in question, as well as saving it to the mentioned file.

Now that you have learned how the above class was defined, pay attention to the following one. It is obviously a subclass, and implements concretely each of the abstract methods declared before.

The signature of this new class is listed below:

// define concrete 'DataSaver' class on top of the chain of
responsibility
class DataSaver extends AbstractDataSaver{
    private $filePath;
    private $data;
    public function __construct($filePath){
        $this->filePath=$filePath;
        $this->data=NULL;
    }
    public function getFilePath(){
        return $this->filePath;
    }
    public function getData(){
        if($this->data==NULL){
            throw new Exception('No data has been set for being
saved to file!');
        }
        return $this->data;
    }
    public function setData($data){
        $this->data=$data;
    }
    public function saveData(){}
}

Undoubtedly, there are some interesting things to be said about the brand new sub class shown above. First, as I said before, it implements the majority of the abstract methods that were initially declared in the base class, and then defines the top of the chain of responsibility that will be implemented by its corresponding child classes.

If this last concept sounds rather confusing to you, please take a look at the definition of the pertinent "getData()" method:

public function getData(){
    if($this->data==NULL){
        throw new Exception('No data has been set for being saved
to file!');
    }
    return $this->data;
}

Essentially, the signature for this method actually consists of an important block within the chain of responsibility schema, since it demonstrates in a nutshell how the absence of the $this->data property (in this case represented by the condition $this->data==NULL) will be handled simply by throwing a new exception, and eventually stopping the application's execution.

As you'll see over the course of the sections to come, all the child classes that will be derived from "AbstractDataSaver" will try to handle the lack of the referenced $this-> data property either on their own or by calling their parent class, instead of triggering an error. This fact really establishes a well-defined chain of responsibility between all the classes; when this condition can't be handled by the current class, it will be delegated to the corresponding parent. Quite good, right?

Having explained the course of action that will be taken by the previous "DataSaver" class when the $this>data property that you learned before is null, it's time to leap forward. In the next section, we will see how the remaining sub classes that integrate the lower levels of the corresponding responsibility chain will be properly defined. 



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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