PHP MySQL Chaining

In my last article I wrote about the new Lambda functions of PHP 5.3. Today I will talk about another cool feature. Since PHP version 5.3 it is possible to let functions return objects. This makes chaining like in JavaScript possible.
It is fairly simple: the function just has to return $this in the end and the next function can be added via ->.

Here is an example:

The whole construct can be used like this:

You can download the full example code here.

For now the functionality of my chaining class is really simple, but it can easily be extended to do more than just building the query string.

In my opinion chaining is most useful in two specific situations:

  1. like in my example for building strings
  2. for filling a data class with data like in other examples.