Web Development

Method Chaining in php

In this post I will explain the very useful feature which introduced in PHP5 as a part of OOP improvement over PHP4. This feature is called method chaining and  you can do nice thing like below:

[cc lang=”php”]
$obj->method1()->method2()->method3();
[/cc]

Well one change between PHP4 and PHP5 is that in PHP5 method can return objects. This small change have made the new way of handling objects and methods in PHP.

In order to implement the method chaining in PHP as explained earlier you must be able to return objects from methods. The returned object does not necessarily need be the same one from which the method is being called.

Regular Use of class

[cc lang=”php”]
name=$name;
}
public function setAge($age=”20″)
{
$this->age=$age;
}
public function intro()
{
echo “Hello, My name is “.$this->name.” and my age is “.$this->age.” years.”;
}
}
?>
[/cc]

Now we can create a object of the class person and by calling the setName and setAge function we can set variable and finally by calling intro method we can introduce the person.

[cc lang=”php”]
setName(“xpertdeveloper”);
$person->setAge(“23″);
$person->intro();
?>
[/cc]

Below is the output of the above code.

[cc lang=”php”]Hello, My name id xpertdeveloper and my age is 23 years.[/cc]

Implementing method chaining

To make the above class to behave for method chaining we have to make only changes to the above class. Actually methods in the above class is not returning anything, but to make chain works we will add return $this; to the end of the both function setName and setAge. So basically this functions will return the object of the class in which functions are placed. So now amended class will look like below:

[cc lang=”php”]
name=$name;
return $this;
}
public function setAge($age=”20″)
{
$this->age=$age;
return $this;
}

public function intro()
{
echo “Hello, My name is “.$this->name.” and my age is “.$this->age.” years.”;
}
}
?>
[/cc]

Ok now in above class all methods have ability to return the object, so it enable us to group method call together in sequence or in chain as below code:

[cc lang=”php”]
setName(“xpertdeveloper”)->setAge(“23”)->intro();
?>
[/cc]

This will also give output same as our previous method.

How it works

If you are confused about what is going on here, then let me explain the code line by line.

First of all check this. [code]$[/code]person[code]->setName(‘xpertdeveloper’). This will call the setName method which will set the name of the person. after this method will return $this, that means it will return the object $person.[/code]

Next comes ->setAge(’23’). Because we are chained with previous method, PHP execute the setAge method of the returned object from the previous class, in our case it is $person. So with this method it will set the age and also return $this means object $person.

at last ->into(); which will print the introduction of the person as per criteria given in the previous methods.

Conclusion : We can make this article in one small sentence, If you return $this then you can chain the method calls together, Done :-)

Benefits : Code readability and reduction in code repetition.

Shares:
  • Lipsey
    Lipsey
    September 26, 2010 at 2:55 am

    Excellent post, l quite agree with your conclusion. However lam having problem subscribing to your rss.

    Reply
  • Shower Cubicle
    January 30, 2011 at 5:27 am

    ~.’ I am really thankful to this topic because it really gives up to date information ::*

    Reply
  • bruce
    January 9, 2012 at 2:42 pm

    in the second snippet you need to add return $this

    Reply
    • bruce
      January 9, 2012 at 2:43 pm

      * by second snippet, I mean second class snippet, ie the last code block

      Reply
  • Bug
    Bug
    January 9, 2012 at 3:27 pm

    You have forgot to add return $this in the amended class …

    Reply
  • Selim
    January 9, 2012 at 3:51 pm

    Hi, Great post.

    However. The code segment you give in the ‘Implementing method chaining’ paragraph and the one you give in the ‘Regular Use of class’ paragraph are exactly the same : There is no return $this statement at the end of each method.

    Reply
  • KL
    KL
    January 9, 2012 at 4:01 pm

    you forgot to change your example code :)

    Reply
  • Andreas Nurbo
    January 9, 2012 at 5:44 pm

    You have included the wrong code in the last code segment. Your set methods does not return any objects.
    Almost all cases of method chaining I’ve seen has been related to some sort of search.
    find(‘object’)->where(‘w=y’)->And(‘i>2’)-exec();

    PS
    The two earlier comments are just spam =).

    Reply
  • t00ny
    t00ny
    January 9, 2012 at 6:35 pm

    Think you forgot the “return $this” in the amended class…

    Reply
  • karl
    karl
    January 9, 2012 at 9:16 pm

    There is no return $this; in the second person class definition.

    Reply
  • Paulo
    January 9, 2012 at 9:24 pm

    You need put “return $this” on each method of second class.

    Reply
  • Stevan Goode
    Stevan Goode
    January 9, 2012 at 10:23 pm

    Hiya,

    In your second class example, did you forget to return $this or am I missing something?

    Steve

    Reply
  • Avinash
    January 9, 2012 at 10:53 pm

    Thanks to all for mentioning the mistake, updated :)

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *