Free cookie consent management tool by TermsFeed Generator PHP Method Chaining | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
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

PHP break and continue
Mar 21, 2023 programming

PHP break and continue

Today we are going to learn about break and continue in PHP. These two words are going to help you a lot when dealing working with loops. you can avoid a lot of extra iterations and save time. ...

5 Min Read Read More
PHP Splat Operator
Feb 15, 2023 programming

PHP Splat Operator

Today we are going to talk about splat operator in PHP. Splat operator is when we add three dots before the name of the variable and it's way more useful than you think. ...

7 Min Read Read More
PHP Callback Functions
Jan 08, 2023 programming

PHP Callback Functions

Today we are going to talk about callback functions in PHP. sometimes we need to pass a function as an argument of another function, that's exactly what callback function is. ...

7 Min Read Read More
Creating a Simple Template Engine like Laravel Blade in PHP
Aug 21, 2023 programming

Creating a Simple Template Engine like Laravel Blade in PHP

Today we are going to learn how to build a simple template engine like Laravel's Blade. it's a great learning opportunity to understand how popular engines like Blade might work under the hood. ...

4 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...