Logo Background RSS

Fluent Interfaces in PHP

  • A new buzzword in the PHP circles is Fluent Interfaces, which is not really new, but a way to chain methods of an object together. Here’s an example from Mike Naberezny which shows the regular way and the Fluent way:

    < ?php private function makeNormal(Customer $customer) {
    $o1 = new Order();
    $customer->addOrder($o1);
    $line1 = new OrderLine(6, Product::find(‘TAL’));
    $o1->addLine($line1);
    $line2 = new OrderLine(5, Product::find(‘HPK’));
    $o1->addLine($line2);
    $line3 = new OrderLine(3, Product::find(‘LGV’));
    $o1->addLine($line3);
    $line2->setSkippable(true);
    $o1->setRush(true);
    }
    ?>

    The fluent way: < ?php private function makeFluent(Customer $customer) {
    $customer->newOrder()
    ->with(6, ‘TAL’)
    ->with(5, ‘HPK’)->skippable()
    ->with(3, ‘LGV’)
    ->priorityRush();
    }
    ?>

    For those who do not recognize this particular buzzword fluent interfaces is a way of chaining methods of an object together. By having a method return a reference to the object itself, return $this; you chain methods together like this $this->methodOne()->methodTwo()->methodThree();. This can make your code easier to read, and that is the point of using fluent interfaces, making your code easier to read.

    via: Zend Developer Zone

    Andi Gutmans tells us where to use this pattern:

    Of course there are no definitive answers but I suggest to consider the following points:
    a) Use your intuition. If you don’t feel this will address the 95% of common use-case for using your interface, then it’s probably not the right solution for what you’re trying to accomplish.
    b) As Paul noted, if in the common use-case you don’t have all the necessary data available to complete the task in one go, you should think twice about doing it.
    c) Probably the most important point: It really has to read well in your language (e.g. English), preferably as a complete sentence. If you can’t read the code out aloud then it’s probably not what you want.

    Link: Andi Gutmans’ Weblog | Fluent Interfaces

    Mark Naberezny has an helloworld example at his blog which shows you how to build a fluent interface:

    In PHP 5 terms, a fluent interface to an object is one where the setter methods return an object handle. It is perhaps simplest to always return $this, however any object handle can be returned. Here’s a simple PHP class that demonstrates how a fluent interface is built:

    < ?php
    class Fluent {
    public function hello() {
    echo ‘hello ‘;
    return $this;
    }
    public function world() {
    echo ‘world’;
    return $this;
    }
    }
    $fluent = new Fluent();
    $fluent->hello()
    ->world();
    ?>

    Link: Fluent Interfaces in PHP

    Bookmark and share:
    • del.icio.us
    • Digg
    • StumbleUpon
    • BlinkList
    • blogmarks
    • Furl
    • Slashdot
    • Spurl
    • Technorati
    • YahooMyWeb
    • description
    • Facebook
    • Google
    • Live
    • Ma.gnolia
    • NewsVine
    • Reddit
    • TwitThis

Advertisement

Leave a Comment