PHP
  Home arrow PHP arrow Page 5 - Introducing the Chain of Responsibility Between PHP Objects
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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!



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT