The method chaining is one of the concepts in the OOP (Object-Oriented Programming) paradigm for binding/chaining method. Here we can call more than one method for a single operation by processing the same object. Each method is a role to change the value of property in the object from one form to another. Therefore the method is often called a mutator.
The benefit of using the chaining method is to make the code more readable. Consider the following code example below.
//without chaining
$object = new Class();
$object->method_one();
$object->method_two();
$object->method_three();
//with chaining
$object = new Class();
$object->method_one()->method_two()->method_three();
Well, instead of calling a method across multiple lines of code, we can create a method chain in a single line of code. Thus the code will look shorter.
To apply the chaining method in PHP as described earlier you should be able to return the object from the method. The returned object does not have to be the same as the method called.
<?php
class someone
{
private $nickname="";
private $born="";
public function setNickName($nickname="")
{
$this->nickname=$nickname;
}
public function setBorn($born="20")
{
$this->born=$born;
}
public function intro()
{
echo "Hello, My nickname is ".$this->nickname." and i was born on ".$this->born." ago.";
}
}
?>
Now we can create the object of the class someone and by calling setNickName and setBorn function. We can set the variable and finally by calling intro method. We can introduce that class someone.
<?php
$someone = new someone();
$someone->setNickName("simpleawesome");
$someone->setBorn("1996");
$someone->intro();
?>
Then type all the above code in the text editor.
Below is the output of the above code.
Information :
$someone->setNickName("simpleawesome"). This serves to call the method setNickName and after this method return $this. It means will return the object.setBorn("1996"). Bound by the previous method, PHP language processing setBorn method of the object returned from the class previous. So will set the year class and restore $this to the object $someone.To create a class like above to follow the method chaining, it takes a change in the class only. To create chain work, we need to add return $this; at the end on both the setNickName and setBorn functions.
setNickNameprevious code
public function setNickName($nickname="")
{
$this->nickname=$nickname;
}
code after additional
public function setNickName($nickname="")
{
$this->nickname=$nickname;
return $this;
}
setBornprevious code
public function setBorn($born="20")
{
$this->born=$born;
}
code after additional
public function setBorn($born="20")
{
$this->born=$born;
return $this;
}
So the class that we are modify it will look like below :
<?php
class someone
{
private $nickname="";
private $born="";
public function setNickName($nickname="")
{
$this->nickname=$nickname;
return $this;
}
public function setBorn($born="20")
{
$this->born=$born;
return $this;
}
public function intro()
{
echo "Hello, My nickname is ".$this->nickname." and i was born on ".$this->born." ago.";
}
}
?>
Now above class all methods have potential to recover the object. We can see the previous object of the class someone.
<?php
$someone = new someone();
$someone->setNickName("simpleawesome");
$someone->setBorn("1996");
$someone->intro();
?>
Then create that object by grouping calls chain with a simple look like below :
<?php
$someone = new someone();
$someone->setNickName("simpleawesome")->setBorn("1996")->intro();
?>
To distinguish files without method chaining with the file contained method chaining, create new file in text editor.
This will give the same output as previous method.
With this method, the code is easy to read and reduces the looping process.