PHP
  Home arrow PHP arrow Page 2 - 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 - The why and how of the chain of responsibility


    (Page 2 of 5 )

    Naturally, it's good to learn how a predefined chain of responsibility can be created across several PHP classes by setting up a comprehensive example. This will help you to grasp the basic concepts of this theme.

    To begin with, I'll define a group of simple classes, tasked with saving different types of data to a specified text file. Each class will be capable of delegating the execution of a particular process to its nearest parent when the current class is incapable of handling the process in question. That being said, here is the definition of the base "AbstractDataSaver" class, which will set up the blueprints for all of its respective sub classes:

    // define abstract 'AbstractDataSaver' class
    abstract class AbstractDataSaver{
        abstract public function getFilePath();
        abstract public function setData($data);
        abstract public function getData();
        abstract public function saveData();

    }

    In this case, the skeleton of the above abstract class exposes the core methods that will be concretely implemented by the respective children. Here, it's clear to see that each of these abstract methods conform to a generic model, which is handy for defining (in a later section) the structure of a few concrete classes that will be capable of storing distinct data on plain text files.

    With reference to the signature of the prior "AbstractDataSaver" class, you can see that it first defines a "getFilePath()" method, which will eventually return the path of the text file to where the data will be saved. Next, it defines a set of additional methods, called "setData()", "getData()" and "saveData()" respectively, tasked with accessing and modifying the data in question, as well as saving it to the mentioned file.

    Now that you have learned how the above class was defined, pay attention to the following one. It is obviously a subclass, and implements concretely each of the abstract methods declared before.

    The signature of this new class is listed below:

    // define concrete 'DataSaver' class on top of the chain of
    responsibility
    class DataSaver extends AbstractDataSaver{
        private $filePath;
        private $data;
        public function __construct($filePath){
            $this->filePath=$filePath;
            $this->data=NULL;
        }
        public function getFilePath(){
            return $this->filePath;
        }
        public function getData(){
            if($this->data==NULL){
                throw new Exception('No data has been set for being
    saved to file!');
            }
            return $this->data;
        }
        public function setData($data){
            $this->data=$data;
        }
        public function saveData(){}
    }

    Undoubtedly, there are some interesting things to be said about the brand new sub class shown above. First, as I said before, it implements the majority of the abstract methods that were initially declared in the base class, and then defines the top of the chain of responsibility that will be implemented by its corresponding child classes.

    If this last concept sounds rather confusing to you, please take a look at the definition of the pertinent "getData()" method:

    public function getData(){
        if($this->data==NULL){
            throw new Exception('No data has been set for being saved
    to file!');
        }
        return $this->data;
    }

    Essentially, the signature for this method actually consists of an important block within the chain of responsibility schema, since it demonstrates in a nutshell how the absence of the $this->data property (in this case represented by the condition $this->data==NULL) will be handled simply by throwing a new exception, and eventually stopping the application's execution.

    As you'll see over the course of the sections to come, all the child classes that will be derived from "AbstractDataSaver" will try to handle the lack of the referenced $this-> data property either on their own or by calling their parent class, instead of triggering an error. This fact really establishes a well-defined chain of responsibility between all the classes; when this condition can't be handled by the current class, it will be delegated to the corresponding parent. Quite good, right?

    Having explained the course of action that will be taken by the previous "DataSaver" class when the $this>data property that you learned before is null, it's time to leap forward. In the next section, we will see how the remaining sub classes that integrate the lower levels of the corresponding responsibility chain will be properly defined. 

    More PHP Articles
    More By Alejandro Gervasio


       · 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 6 hosted by Hostway
    Stay green...Green IT