Method chaining is a useful technique that helps you to write more compact and powerful code. In this first part of a series that introduces you to this programming methodology, you'll learn how to define and use chainable methods in PHP 5. The process is quite simple to grasp, even if you have only an intermediate background in using the object-oriented paradigm.
Over the course of the last few years PHP has reinvented itself; that's why it's the mature language that we see nowadays. The most significant change introduced by the PHP development team is the release of PHP 5, which incorporates a robust object model, a good exception handling mechanism, type hinting, the filter library, and many other features that you've probably used hundreds of times before.
While it's true that PHP 5 does allow you to implement the object-oriented paradigm in a much more efficient and elegant manner, other fundamental concepts inherent to software programming can be applied to both PHP 4 and PHP 5 similarly.
To cite a few examples, common design patters like Factory, Singleton and Model-View-Controller (add your own to the list) can be implemented pretty easily in both versions of PHP, since they have to do more with proven programming methodologies than with the language's intrinsic features.
One great technique that can be used when developing PHP applications is popularly known as method chaining. For those just learning PHP, it may sound intimidating, particularly if they're not familiar with classes and objects and their methods, obviously. But the truth is that method chaining is an approach that can be mastered with minor efforts.
But, what is method chaining? Well, let me introduce you to the subject gently with a simple example that you'll quickly grasp. Say that you're building a MySQL abstraction class that implements some methods that will be used for performing queries against a selected database table.
In a typical situation, there will be only one method that will run SELECT statements, which should be used in conjunction with common query modifiers (such as WHERE, LIKE, LIMIT and ORDER BY SQL clauses) to perform conditional queries. However, it's possible to implement multiple methods instead, where each one be will be responsible only for executing raw SELECTS, WHERE and ORDER BY clauses, and so forth.
With these methods coded in that way, it's feasible to call them chained according to specific needs, and get a result set which can be processed in other clever ways. This chaining process can be easily accomplished by making the methods return an instance of the MySQL abstraction class to calling code. Simple and effective.
High-quality frameworks like Kohana and CodeIgniter use method chaining within their database classes. In fact, Kohana will let you use method chaining with many other classes as well. Method chaining makes the code more compact and robust.
So, assuming that you're interested in learning how to use method chaining within your own PHP applications, in this series of articles I'm going to create some examples that will show you how useful this approach can be. I'm going to start by developing a trivial string processor class to demonstrate the implementation of this approach. When I complete this application, I'll build more complex programs. Now, let's start discovering the real power behind method chaining in PHP 5!