PHP
  Home arrow PHP arrow 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
    ( 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
     

       

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