PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Completing the chain of responsibility
    ( Page 4 of 5 )

    In this case, since I wish to have at hand another sub class that eventually moves the program's execution up the chain of responsibility, I'll simply define a new child class, which will be tasked with saving plain strings to a given text file. Not surprisingly, the signature of this class is extremely simple and looks like this:

    // define 'StringSaver' class
    class StringSaver extends AbstractDataSaver{
        private $filePath;
        private $data;
        private $parentDataSaver;
        public function __construct($filePath,DataSaver
    $parentDataSaver){
            $this->filePath=$filePath;
            $this->data=NULL;
            $this->parentDataSaver=$parentDataSaver;
        }
        // get path for data file
        public function getFilePath(){
            return $this->filePath;
        }
        // get string of data
        public function getData(){
            if($this->data==NULL){
                // call parent object in chain of responsibility
                return $this->parentDataSaver->getData();
            }
            else{
                return $this->data;
            }
        }
        // set string of data
        public function setData($data){
            if(!is_string($data)){
                throw new Exception('Data must be a string!');
            }
            $this->data=$data;
        }
        // save string to file
        public function saveData(){
            if(!$fp=fopen($this->getFilePath(),'w')){
                throw new Exception('Error opening data file!');
            }
            if(!fwrite($fp,$this->getData())){
                throw new Exception('Error saving array to data
    file!');
            }
            fclose($fp);
        }
    }

    If you consider that the logic implemented by the previous "ArraySaver" sub class was really easy to understand, then you'll find the behavior followed by the above class even simpler.

    Please notice how its "getData()" method also calls the corresponding parent class (inputted via the respective constructor) when it's not possible to handle the absence of the $this->data property. This is nearly identical to the approach followed by the other "ArraySaver" sub class, defined opportunely in the previous section.

    All right, I strongly believe that the two subclasses that you learned before are more than enough to give you a correct understanding of how a specific chain of responsibility can be established between a group of PHP classes.

    Considering this situation, in the final section of this article I'll show you how all these classes can interact with each other, implementing the chain of responsibility that I discussed right from the very beginning of this article.

    Want to learn how this will be achieved? Then go ahead and click on the link below.



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

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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 ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek