In the previous tutorial, we learned to access the non-static member of class using $this keyword. In this chapter we will learn that, when a class’s methods return the $this keyword, they can be chained together.

For instance in our Animal class, we have set the values of family and food for cow and lion object.

In order to achieve our goal, we will be using the private property $familyName and $foodName to our class.

<?php
//declare class
class Animal {

  //Properties
  private $familyName;
  private $foodName;

}
?>

We have to add three methods to our class.

  1. The familyName() methods to add the family value
  2. The foodName() methods to add the food value
  3. The result() methods to print the result
class Animal {

  //Properties
  private $familyName;
  private $foodName;

  //set the family name
  public function familyName($family){
  	$this->familyName = $family;
  	return $this;
  }

  //set the food name
  public function foodName($food){
  	$this->foodName = $food;
  	return $this;
  }

  //print the result
  public function result(){
	echo "I eat " . $this->foodName . " and belongs to family " .$this->familyName;
  }

}

Notice that we return $this pseudo-variable between familyName() method and foodName() method. And we know that “$this” pseudo-variable originally contained the object of the current class. 

As we would like our code to look elegant, we will chain the method and properties. Note the arrows on the code.

$cow->familyName('Herbivore')->foodName('Grass')->result();

In word, the familyName(‘Herbivore’) job is to call the familyName method and set “Herbivore” to familyName property as value. Then familyName() method will return $this, that means it will return the object cow.

Next let’s go to ->foodName(‘Grass’). As we got the chain object from the previous familyName() method, So, with that cow object, we can call foodName(‘Grass’) and set the name of food to Grass as value in foodName property. Again here we get $this as return, Similarly, here we will get chain as return to our object cow.

Lastly by ->result() method we can get the chain result for the previous series.

Now, we can create an object from the Animal class with the name of $cow and add the family and food name.

//Create a new object from the animal class
$cow = new Animal();

//add the family name and food name then get the result
$cow->familyName('Herbivore')->foodName('Grass')->result();
Result:
I eat Grass and belongs to family Herbivore.

This is how the full code looks like:

<?php
//declare class
class Animal {

	//Properties
  private $familyName;
  private $foodName;

  //set the family name
  public function familyName($family){
  	$this->familyName = $family;
  	return $this;
  }

  //set the food name
  public function foodName($food){
  	$this->foodName = $food;
  	return $this;
  }

  public function result(){
  	//print the results to the screem
	echo "I eat " . $this->foodName . " and belongs to family " .$this->familyName;
  }

}

//Create a new object from the animal class
$cow = new Animal();

$cow->familyName('Herbivore')->foodName('Grass')->result();

?>

Conclusion

In this tutorial, you learned that you can chain properties and methods to provide for a fluent code and improve its readability.

If you have any questions, feel free to ask in the comments below .

If this tip has been helpful to you, please spend a second of your time and share it using the buttons below… thanks!

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *