PHP Method Chaining in OOP

PHP Method Chaining in OOP

PHP Method Chaining in OOP

Hello Everyone, in this article we will learn  PHP Method Chaining in OOP.

What is Method Chaining?

In simple words, Method Chaining is a way to keep calling multiple methods in a single line one after another such as:

Lets say our class name is Something. So we will create an object from that class.

class Something {

  public function doSomething() {
    // Do stuff
    return $this;
  }

  public function doSomethingElse() {
    // Do more stuff
    return $this;
  }

}

Lets create Object from this class:

$my_object = new Something();

Now with the help of method chaining, we can call the methods like:

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

 

In many cases, we need to use several method to transform a value. Lets say, we have a Student Class. In that class, there is three private properties such as: Name, Roll and Class. We have three setter method, eg: setName(), setRoll(), and setClass() . Also we have a getter method named getStudentInfo(). Please take a look at the code below:

 

[php]

<?php
class Student
{
private $name = "";
private $roll = "";
private $class = "";

public function setName($name = "")
{
$this->name = $name;
}

public function setRoll($roll = "")
{
$this->roll = $roll;
}

public function setClass($class = "8")
{
$this->class = $class;
}

public function getStudentInfo()
{
echo "Student Info: Name is ".$this->name." , roll is ".$this->roll." and reads in class ",$this->class;
}
}

$student = new Student();
$student->setName("John Doe");
$student->setRoll("33");
$student->setClass("3");
$student->getStudentInfo();
?>

[/php]

Explanation: Here we can see there are three setter method and one getter method. When we are creating object, we are calling three individual setter methods for setting these values and calling a getter method for getting info. But using “Method Chaining“, we can do these in a single line. Let’s check that out.

Same code if we want to write with the help of method chaining, it can be written as:

[php]

<?php
class Student
{
private $name = "";
private $roll = "";
private $class = "";

public function setName($name = "")
{
$this->name = $name;
return $this;
}

public function setRoll($roll = "")
{
$this->roll = $roll;
return $this;
}

public function setClass($class = "8")
{
$this->class = $class;
return $this;
}

public function getStudentInfo()
{
echo "Student Info: Name is ".$this->name." , roll is ".$this->roll." and reads in class ",$this->class;
}
}

$student = new Student();
$student->setName(‘Jane Doe’)->setRoll(’33’)->setClass(‘3’)->getStudentInfo();
// $student->setName("John Doe");
// $student->setRoll("33");
// $student->setClass("3");
// $student->getStudentInfo();
?>

[/php]

Here you can see, After creating the object, we are calling all these getter and setter methods in  a single line by using method chaining. Before that, we had to return “$this” object instance for every method. It looks cleaner now.

Method chaining improves code readability. But it may make debugging bit more trickier.

 

That’s all for today! Hope See you in next tutorial.

Editorial Staff

A Learner and trying to share what I learned!