Travis Swicegood

Fluent-API, here I come!

One of the things that bugs me about PHP is that fluent APIs that were made possible by method chaining in PHP 5 are hindered by the new keyword. In Python or Javascript it's possible to instantiate the object and continue chaining it. An example in PHP syntax would be:

$result = new SQL()->select('*')
                   ->from('users')
                   ->where('username')
                   ->like('travis%')
                   ->execute();

In PHP, that "new" kills it. However, this code would work:

$result = SQL()->select('*')
               ->from('users')
               ->where('username')
               ->like('travis%')
               ->execute();

All you have to do is define a function that has the same name as your class that wraps the new and off you go. This falls into that "I guess I should have known" category. Functions and classes aren't the same in PHP and since it will never going down the everything is an object road (unfortunately - if you've ever programmed in a language that has it you know what I mean) it would make sense that there wouldn't be any name clashing between a function and class of the same name.

This means I can start removing all of the factory methods I have that do nothing but instantiate an object and return it. Now I'll just slap a function in and away we go.

As an extra benefit, interfaces fall into the same category as classes. You can't have an interface and an object both named SQL, but either one with a function is fine. This gives all sorts of new possibilities to a Dependency Injection Manager:

interface SQL {
    public function query($query);
}
$sql = SQL();
$sql instanceof SQL_Driver_MySQL;

Now, if I could just find a use for a DI manager that couldn't be solved by other means. :-)

About

Travis Swicegood is a professional programmer and owner of Domain51, a web development company with a focus on non-profits, NGOs, and online activists. He doesn't change the world, he supports those who do.

He has personal a focus on web applications, performance, and stability; is author of Pragmatic Version Control using Git; and working on his second book. He has been using PHP; since '99 and still remembers how revolutionary PHP 4 was, but can't remember why. He's a TDD, open-source, and open government advocate—sometimes called a zealot—and lurker on many an open-source project mailing list when not learning other programming; languages; for fun, exploring his surroundings on bike, or tasting his latest kitchen and home-brew creations.

Contact
Around the Internet