Home arrow PHP arrow Introducing the Chain of Responsibility Between PHP Objects

Introducing the Chain of Responsibility Between PHP Objects

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.

TABLE OF CONTENTS:
  1. Introducing the Chain of Responsibility Between PHP Objects
  2. The why and how of the chain of responsibility
  3. Creating a few sub classes
  4. Completing the chain of responsibility
  5. Making the chained classes work together
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
October 30, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: