Learn Simple Method Chaining in PHP

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 Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

6 thoughts on “Learn Simple Method Chaining in PHP

Comments are closed.