Breaking

Monday, February 10, 2020

PHP Method Chaining

PHP method chaining

One of the major changes between PHP4 and PHP5 is that in PHP5 method can return objects.
As the name indicates, method chaining is nothing but executing multiple methods in a single call.
This means in regular method calling we write code as follows-
class studentDetails {

 public $id = 0;
 public $name = '';
 public $address ='';

 public function setStudentId($id) {
  $this->id = $id;
 }

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

 public function setStudentAddress($address) {
  $this->address = $address;
 }
}

$obj = new studentDetails();
$obj->setStudentId(1);
$obj->setStudentName('sachin tendulkar');
$obj->setStudentAddress('Mumbai');
In the above example, we have created class name studentDetails and then in the class, we have created three functions to set id, name, and address of the student respectively. Then by creating an object of studentDetails, we called each individual method as per our requirement. This is how the general flow of method calls works.
In method chaining, there is a methodology of call multiple functions in a single call. Let's look into the below example first and then we will look into it in detail.
class studentDetails {
 public $id = 0;
 public $name = '';
 public $address ='';

 public function setStudentId($id) {
  $this->id = $id;
  return $this;
 }

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

 public function setStudentAddress($address) {
  $this->address = $address;
  return $this;
 }

 public function getStudentInfo() {
  return 'student '. $this->name. ' of id '.$this->id. ' stays at '. $this->address;
 }
}

$obj = new studentDetails();
echo $obj->setStudentId(1)->setStudentName('sachin tendulkar')->setStudentAddress('Mumbai')->getStudentInfo();

Here, like the first example, we have created studentDetails class and defined the same methods as above with one additional method named getStudentInfo() which takes a value of each variable and creates a string which is then returned.
In addition, we've added a return statement in each of the three methods because this will return an object ($this) which contains each variable's value. Firstly, all the values in the individual variable is blank but as we call one method at a time and then immediately after that another method and likewise then-current object gets updated with variable value and finally all the object variables contains required values and then finally getStudentInfo() method returns the string with all variables value.

Why we need method chaining?

Here is the big question, if we can call the individual methods as per our requirement then why we need to add return statement in each method and call it in the chain.
But the answer is simple.
We can only use method chaining where we need current objects all the information whether that contains a string, integer, boolean or any object.

Method chaining in child class:

Let's extend the same example with one parent class named student.
class student{
 public $studentAttendance = 0;

 public function setStudentAttendance($attendance) {
  $this->studentAttendance = $attendance;
  return $this;
 }
}

class studentDetails extends student {
 public $id = 0;
 public $name = '';
 public $address ='';

 public function setStudentId($id) {
  $this->id = $id;
  return $this;
 }

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

 public function setStudentAddress($address) {
  $this->address = $address;
  return $this;
 }

 public function getStudentInfo() {
  return 'student '. $this->name. ' of id '.$this->id. ' stays at '. $this->address. ' having an attendance of '. $this->studentAttendance;
 }
}

$obj = new studentDetails();
echo $obj->setStudentId(1)->setStudentName('sachin tendulkar')->setStudentAddress('Mumbai')->setStudentAttendance(100)->getStudentInfo();
Here, we can see that the class student has one method setStudentAttendance() which takes one parameter $attendance and set it to $studentAttendance variable. Now in the child class, we have called variable $studentAttendance using $this->studentAttendance and as we know that all the methods and variables of the parent class are available to the child directly because we have extended it to the child class (except private). Hence, there is no separate/additional functionality need to do for method chaining when there is a scenario of parent-child.

Static Method Chaining

In case of static method chaining, there are no additional changes need to do in the class apart from converting return $this; to return new static;
This is because we know that, inside a static function, there is no existence of $this because static method belongs to class level and not object level.
Let's take the below example for our understanding of static method chaining.
class studentDetails {

 public static $id = 0;
 public static $name = '';
 public static $address ='';

 public static function setStudentId($id) {
  static::$id = $id;
  return new static;
 }

 public static function setStudentName($name) {
  static::$name = $name;
  return new static;
 }

 public static function setStudentAddress($address) {
  static::$address = $address;
  return new static;
 }
 
 public static function getStudentInfo() {
  return 'student '. static::$name. ' of id '.static::$id. ' stays at '. static::$address;
 }
}

echo studentDetails::setStudentId(1)->setStudentName('sachin tendulkar')->setStudentAddress('Mumbai')->getStudentInfo();
Here, calling methods will remain same (in the static method chaining) means using ->
.
This way we have understood what method chaining is, how we can use it in the codebase, different ways of using method chaining.

No comments:

Post a Comment