Tuesday, February 8, 2011

Learn Simple Method Chaining in PHP

2/08/2011

In this article we will learn an advanced OOPS programming concept in PHP known as Method chaining. If you have worked on PHP frameworks like Zend, Magento or CakePHP, you might have noticed a very convinient style of accessing methods of different classes, It looks as:
$obj->foo()->bar()->anotherMethod();
This type of programming technique is known as method chaining. If you are thinking how is it possible to perform this chaining, then don't worry. Today we will learn this fancy concept.


Method Chaining in Simple Classes:
class Person
{
    private $name;
    private $age;
    
    public function setName($Name)
    {
        $this->name = $Name;
    }
    
    public function setAge($Age)
    {
        $this->age = $Age;
    }
    
    public function findMe()
    {
 echo "My name is ".$this->name." and I am ".$this->age. " years old.";
    }
}

To access this class we will create an object of Person class:
$myself = new Person();
$myself->setName('Arvind Bhardwaj');
$myself->setAge('22');
$myself->findMe();

This example will print:
My name is Arvind Bhardwaj and I am 22 years old.

Introducing Method Chaining:
To enable method chaining in our previous example, we need to add only a single line of code in each 'setXXX' function. And that code is return $this;.
Now our class looks like:
class Person
{
    private $name;
    private $age;
    
    public function setName($Name)
    {
        $this->name = $Name;
 return $this;//Returns object of 'this' i.e Person class
    }
    
    public function setAge($Age)
    {
        $this->age = $Age;
 return $this;//Again returns object of 'this' i.e Person class
    }
    
    public function findMe()
    {
 echo "My name is ".$this->name." and I am ".$this->age. " years old.";
    }
}

Now lets access our class methods through method chaining:
$myself = new Person();
$myself->setName('Arvind Bhardwaj')->setAge('22')->findMe();

Explanation of concept:
Surely you're a bit confused about precisely what is going on here. Lets go through this code in an easy way. Before that remember that method chaining always works from left to right!
$myself = new Person() creates a new object of the Person class, quite easy to guess though.
Next, $myself->setName('Arvind Bhardwaj') assigns the name to a variable and returns the object of the same class.
Now $myself->setName('Arvind Bhardwaj') has become an object of the Person class, so we can access the Person class by using $myself->setName('Arvind Bhardwaj') as an object.
Now we set the age as $myself->setName('Arvind Bhardwaj')->setAge('22'). setAge() again returns the object of this class, so the complete phrase $myself->setName('Arvind Bhardwaj')->setAge('22') is now an object of Person.
Finally we print the user information by accessing findMe method as:
$myself->setName('Arvind Bhardwaj')->setAge('22')->findMe();

This is how method chaining works.

Method Chaining in Multiple Classes:
If we have more than one class in our project, method chaining can ease our life a lot in such situation.
Consider following example:
<php
class ComplexPerson
{
    public function setName($Name)
    {
 return new FindNames($Name);//Returns object of FindNames class.
    }
    
    public function setAge($Age)
    {
        $this->age = $Age;
 return new FindAges($Age);//Returns object of FindAges class.
    }
}

class FindNames
{
    private $name;

 public function __construct($n)
 {
   $this->name = $n;
 }
 
 public function printName()
 {
   echo "I am ".$this->n.".";
 }
}

class FindAges
{

 public function __construct($a)
 {
   $this->age = $a;
 }
 
 public function printAge()
 {
   echo "I am ".$this->age." years old.";
 }
}
?>

To use this cross-class method chaining, we have to write:
$anotherMe = new ComplexPerson();
$anotherMe->setName("Arvind Bhardwaj")->printName();
$anotherMe->setAge("22")->printAge();

Hope you enjoyed this article. Best of luck:)

Written by

Arvind is a web developer, programmer and blogger. He has expertise in PHP, Magento, WordPress, jQuery, JavaScript, HTML5 and CSS3. He loves to develop good looking websites with strong backend.

6 comments :

  1. nice post :-)

    thanks for the post.

    ReplyDelete
  2. Thank you very much :)

    Very interesting article. I'll experiment with method chaining tonight.

    ReplyDelete
  3. Nice one. Nice and easy explanation.

    ReplyDelete
  4. really awesome...

    ReplyDelete
  5. vHi,
    To return the class instance in the static method (to chain it), you can use:
    return new self; .
    Here is an example:
    http://coursesweb.net/php-mysql/chaining-static-public-class-methods-php_cs

    ReplyDelete

We would love to hear from you...

 

© 2014 Web Speaks . All rights resevered. Designed by Templateism