PHP
  Home arrow PHP arrow Page 5 - Introducing the Chain of Responsibilit...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Introducing the Chain of Responsibility Between PHP Objects
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2006-10-30

    Table of Contents:
  • Introducing the Chain of Responsibility Between PHP Objects
  • The why and how of the chain of responsibility
  • Creating a few sub classes
  • Completing the chain of responsibility
  • Making the chained classes work together

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

       · In this first part of the series, you'll learn how to apply the chain of...
       · why aren't ArrayDataSaver & StringDataSaver children of DataSaver? That would...
       · Hello Tom,Thank you for posting your comments on my PHP article. Concerning your...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT