HomePHP 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.
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!