PHP Method Chaining

PHP Method Chaining

Last Updated on Mar 22, 2023

Introduction

Do you remember when we talked about classes?

Here I have a class Human

<?php
class Human
{
   private $name;
   private $age;
 
   public function setName($name)
   {
       $this->name = ucfirst($name);
   }
 
   public function setAge($age)
   {
       $this->age = $age;
   }
 
   public function introduce()
   {
       return 'Hello my name is '.$this->name.' and I am '.$this->age.' years old.';
   }
}
$pratham = new Human();
$pratham->setName('pratham');
$pratham->setAge(23);
echo $pratham->introduce();

When we want to call the setName and setAge method, we should call them from our object. That’s okay but I’d like to change it to something like this:

$pratham->setName('pratham')
       ->setAge(23)
       ->introduce();

It’s cleaner and more readable and I don’t have to refer to my object.

 

At the moment if I write that I will get an error so how can I achieve that?

 

Method Chaining

It’s very easy.

At the end of the methods that are going to be chained return the object itself. Like this:

public function setAge($age)
{
   $this->age = $age;
   return $this;
}

Let’s look at the code again

class Human
{
   private $name;
   private $age;
 
   public function setName($name)
   {
       $this->name = ucfirst($name);
       return $this;
   }
 
   public function setAge($age)
   {
       $this->age = $age;
       return $this;
   }
 
   public function introduce()
   {
       return 'Hello my name is '.$this->name.' and I am '.$this->age.' years old.';
   }
}

I don’t want to chain introduce that’ why I didn’t return the object and instead returned my message.

Now I can run it without any error

$pratham = new Human();
echo $pratham->setName('pratham')
            ->setAge(23)
            ->introduce();
// Hello my name is Pratham and I am 23 years old.

When you chain the methods like this the order of the methods that return the object doesn’t matter unless your functions depend on each other internally.

So both of these works

$pratham->setName('pratham')
        ->setAge(23)
        ->introduce();

$pratham->setAge(23)
       ->setName('pratham')
       ->introduce();

Here is all of our code:

<?php
class Human
{
   private $name;
   private $age;
 
   public function setName($name)
   {
       $this->name = ucfirst($name);
      
       return $this;
   }
 
   public function setAge($age)
   {
       $this->age = $age;
      
       return $this;
   }
 
   public function introduce()
   {
       return 'Hello my name is '.$this->name.' and I am '.$this->age.' years old.';
   }
}
$pratham = new Human();
echo $pratham->setAge(23)
            ->setName('pratham')
            ->introduce();
// Hello my name is Pratham and I am 23 years old.
https://youtu.be/HUzG5iSktLU

Conclusion

Now you know about method chaining in PHP.

I recommend you to open a PHP files and try writing a class. add methods and then try to chain all of them.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • chaining methods in PHP
  • return the object itself

Category: programming

Tags: #php #tips and tricks

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Courses