PHP
  Home arrow PHP arrow Introducing the Chain of Responsibilit...
Dev Shed Forums 
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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


    (Page 1 of 5 )

    This article, the first of three parts, introduces you to the chain of responsibility pattern in PHP 5. This pattern is useful when a specific class cannot deal with a particular task, and must therefore transfer a program's execution to another class with the appropriate capability.

    Very often during the development of a web application with PHP, you probably found yourself creating a wealth of classes that have a predefined scope of action where they will be instantiated, and present a carefully-crafted grade of responsibility as well. Although most of the time, this scenario makes more sense than having a well-planned interaction between different objects, there are situations where things aren't so clear, particularly when trying to define the range of specific tasks that must be performed by certain classes.

    To put this into perspective, let me cite a concrete example that hopefully will allow you to understand more clearly what I'm talking about here. Suppose that you have created a useful MySQL wrapping class, which obviously is capable of connecting to the server, running queries, selecting databases and so forth. So far, this case is quite familiar to you, isn't it? Now let me go one step further and ask you the following question: what's the best approach to take when it comes to handling result sets?

    Is it better to implement the logic required for processing data sets as part of the same class, or to build a new class that will handle independently all the returned database rows?

    Certainly, from my personal point of view, I'd follow the latter approach, simply because it allows the scope of responsibility established between the mentioned classes to be much better delimited, as well as their respective interdependencies. However, when building an isolated result set processing class, this approach might not eventually be capable of dealing with certain conditions, such as formatting database rows as XML, to cite a concrete example.

    Therefore, it would be necessary to create another class that would be responsible for returning to calling code differently-formatted result sets to keep the scope of all the classes clearly defined. Now, do you grasp my point with this example? As you can see, there are times when a specific class may not be capable of dealing with a given task, and therefore must transfer the program's execution to a different one which potentially will solve the problem in question.

    True to form, the situation that I just described above is commonly known by many programmers as the "chain of responsibility between objects" or more simply, the chain of responsibility pattern. It deserves an in-depth look to explore its advantages in real situations.

    Therefore, in this three-part series, I'll show you how to create a chain of responsibility across different classes, which hopefully will give you a better understanding of how this schema can be implemented with PHP. Are you ready to learn more on this topic? All right, let's go!

    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

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway