PHP
  Home arrow PHP arrow Page 3 - 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 - Creating a few sub classes
    ( Page 3 of 5 )

    Creating the respective sub classes that reside on the lower levels of the mentioned responsibility chain is in fact a straightforward process that can be performed with minor problems. This procedure is limited to deriving a pair of child classes from the "AbstractDataSaver" class shown in the previous section, and coding their respective "getData()" methods in such a way that they can delegate the program flow to the parent class when they can't handle the absence of the mentioned $this->data property by themselves.

    Of course, you'll understand this concept much better if you take a look at the definition of the new sub class below:

    // define 'ArraySaver' class
    class ArraySaver 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 array 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 array of data
        public function setData($data){
            if(!is_array($data)){
                throw new Exception('Data must be an array!');
            }
            $this->data=$data;
        }
        // save array to file
        public function saveData(){
            if(!$fp=fopen($this->getFilePath(),'w')){
                throw new Exception('Error opening data file!');
            }
            if(!fwrite($fp,serialize($this->getData()))){
                throw new Exception('Error saving array to data
    file!');
            }
            fclose($fp);
        }
    }

    After listing the definition of the above "ArraySaver" class, you'll probably know what it does. To put it simply, this new sub class takes up an input array and saves it to a given text file. Simple, right? However, I'd like you to pay careful attention to the signature of all its methods, but more specifically to the following one:

    public function getData(){
        if($this->data==NULL){
            // call parent object in chain of responsibility
            return $this->parentDataSaver->getData();
        }
        else{
            return $this->data;
        }
    }

    As you can see, the "getData()" method handles the absence of the $this->data property in a slightly different manner than the one shown when the parent "DataSaver" class was defined. More specifically, this method will call the mentioned parent when it is unable to handle this condition on its own. In this way it transfers the responsibility for dealing with the problem in question toward the upper element placed inside the chain.

    Now do you realize how the a predefined responsibility chain can established across several PHP classes? I bet you do!

    At this stage, and after understanding properly how the previous sub class works, let's move on to the following section. I will show you how another child class can be created so that we have at least two elements that implement the chain of responsibility that I discussed before.

    Sounds interesting, doesn't it? Click on the below link and keep reading.



     
     
    >>> 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 4 Hosted by Hostway
    Stay green...Green IT