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

Making the chained classes work together - 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

As I said in the section you just read, the simplest way to see whether the chain of responsibility really works consists of developing a concrete example where all the previously defined classes are put to work conjunctly. Therefore, I'll begin listing a short script. It will show the behavior followed by the first class, that is "DataSaver," before and after assigning a value to the $this->data property. Please take a look:

try{
    // instantiate 'DataSaver' object
    $dataSaver=new DataSaver('defaultPath/default_datafile.txt');
    // call 'getData()' method without setting its $data property
    echo 'DataSaver class outputs the following without setting data property:<br />';
    $dataSaver->getData();
    /* displays the following:
    DataSaver class outputs the following without setting data
property:
    No data has been set for being saved to file!
    */
    // assign a value to $data property
    echo 'DataSaver class outputs the following when $data
property has been set:<br />';
    $dataSaver->setData('This is the string of data that will be
saved to file.');
    echo $dataSaver->getData();
    /* displays the following:
    DataSaver class outputs the following when $data property has
been set:
    This is the string of data that will be saved to file.
    */
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

As you can see, before assigning a value to the $this->data property, the script throws an exception and its execution is halted. On the other hand, when a new value is assigned to the property in question, the snippet displays an indicative message. Since the "DataSaver" class is seated on top of the responsibility chain, the behavior shown above is simply expected.

Now, take a deep breath and look at the following two examples, which demonstrate how the corresponding child classes delegate the responsibility to their parent when they can't deal with the specific situation where the value assigned to the $this->data property is null:

try{
    // instantiate 'DataSaver' object
    $dataSaver=new DataSaver('defaultPath/default_datafile.txt');
    // instantiate 'ArraySaver' object
    $arraySaver=new ArraySaver('defaultPath/default_datafile.txt',$dataSaver);
    // try to save data without setting $data property
    echo 'DataSaver class outputs the following without setting
the value of $data property:<br />';
    $arraySaver->saveData();
    /*
    // displays the following:
    DataSaver class outputs the following without setting the
value of $data property:
    No data has been set for being saved to file!
    */
    // assign value to $data property
    echo 'ArraySaver class outputs the following when $data
property has been set:<br />';
    $arraySaver->setData(array('This is element 1','This is
element 2','This is element 3'));
    $arraySaver->saveData();
    print_r($arraySaver->getData());
    /*
    displays the following:
    ArraySaver class outputs the following when $data property
has been set:
    Array ( [0] => This is element 1 [1] => This is element 2 [2]
=> This is element 3 )
    */
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}
try{
    // instantiate 'DataSaver' object
    $dataSaver=new DataSaver('defaultPath/default_datafile.txt');
    // instantiate 'StringSaver' object
    $stringSaver=new StringSaver('defaultPath/default_datafile.txt',$dataSaver);
    // try to save data without setting $data property
    echo 'StringSaver class outputs the following without setting
the value of $data property:<br />';
    $stringSaver->saveData();
    /*
    displays the following:
    StringSaver class outputs the following without setting the
value of $data property:
    No data has been set for being saved to file!
    */
    // assign value to $data property
    echo 'ArraySaver class outputs the following when $data
property has been set:<br />';
    $stringSaver->setData('This string of data will be saved to
file.');
    $stringSaver->saveData();
    echo $stringSaver->getData();
    /*
    displays the following:
    ArraySaver class outputs the following when $data property
has been set:
    This string of data will be saved to file.
    */
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

Indeed, after examining the above examples, you'll have to agree with me that the chain of responsibility works like a charm! Try creating your own PHP classes and implement the same schema to see what happens in each case.

Wrapping up

In this first article of the series, you learned the basics of defining a chain of responsibility between several PHP classes to handle a specific task. Hopefully, the hands-on examples shown here will give you some useful pointers to get started on this subject quickly.

In the next tutorial, I'll show you how to apply a responsibility chain across different PHP classes, which is handy for developing an error login system. Meet you in the next part!



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