views:

126

answers:

3

Hi,

I am using PHP5, and heard of a new featured in object-oriented approach, called method chaining.

Does any one know what it is?

I want to know how to implement method chaining using PHP5 with object-oriented approach.

Thanks

+9  A: 

Its rather simple really, you have a series of mutator methods that all returns the original (or other) objects, that way you can keep calling functions.

<?php
class fakeString
{
    private $str;
    function __construct()
    {
        $this->str = "";
    }

    function addA()
    {
        $this->str .= "a";
        return $this;
    }

    function addB()
    {
        $this->str .= "b";
        return $this;
    }

    function getStr()
    {
        return $this->str;
    }
}


$a = new fakeString();


echo $a->addA()->addB()->getStr();

This outputs "ab"

Kristoffer S Hansen
+1 [mutator methods](http://en.wikipedia.org/wiki/Mutator_method)
BoltClock
@Kristoffer S Hansen :-- thanks buddy !!! this tect is good. i like it
Sanjay
This is also sometimes referred to as Fluent Interface
Nithesh Chandra
return $this; Is it mean return an object?
Dezigo
@Nitesh that is incorrect. [Fluent Interfaces](http://www.martinfowler.com/bliki/FluentInterface.html) use [Method Chaining](http://martinfowler.com/dslwip/MethodChaining.html) as their primary mechanism, but [it's not the same](http://en.wikipedia.org/wiki/Fluent_interface). Method chaining simply returns the host object, while a Fluent Interface is aimed at creating a [DSL](http://en.wikipedia.org/wiki/Domain-specific_language). Ex: `$foo->setBar(1)->setBaz(2)` vs `$table->select()->from('foo')->where('bar = 1')->order('ASC)`. The latter spans multiple objects.
Gordon
@Dezigo, returning `$this` is returning the current object, but you could return any object, that would then be the object you are calling methods on.
Kristoffer S Hansen
+2  A: 

Method chaining means that you can chain method calls like this:

$object->method1()->method2()->method3()

This means that method1() needs to return an object, and method2() is given the result of method2(). Method2() then passes the return value to method3().

Good article: http://www.talkphp.com/advanced-php-programming/1163-php5-method-chaining.html

alexn
The explanation is a bit off. The return values are not passed around. The methods simply return the host object.
Gordon
@Gordon Well, the host object is not returned. Any object can be returned and chained.
alexn
Then I would argue it's not method chaining as defined by Fowler, e.g. [Make modifier methods return the host object so that multiple modifiers can be invoked in a single expression.](http://martinfowler.com/dslwip/MethodChaining.html) - if you return other objects, it's more likely a Fluent Interface :)
Gordon
@Gordin Point taken :)
alexn
+4  A: 

Basically, you take an object:

$obj = new ObjectWithChainableMethods();

Call a method that effectively does a return $this; at the end:

$obj->doSomething();

Since it returns the same object, or rather, a reference to the same object, you can continue calling methods of the same class off the return value, like so:

$obj->doSomething()->doSomethingElse();

That's it, really. Two important things:

  1. As you note, it's PHP 5 only. It won't work properly in PHP 4 because it returns objects by value and that means you're calling methods on different copies of an object, which would break your code.

  2. Again, you need to return the object in your chainable methods:

    public function doSomething() {
        // Do stuff
        return $this;
    }
    
    
    public function doSomethingElse() {
        // Do more stuff
        return $this;
    }
    
BoltClock
alex
@alex: I don't have PHP 4 to test with right now, but I'm pretty sure not.
BoltClock
@BoltClock I didn't think so either, but it *should* work right? Perhaps if PHP4 wasn't so PHP4-ish.
alex