Introducing the Chain of Responsibility Between PHP Objects - Making the chained classes work together
(Page 5 of 5 )
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!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |