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

Ultimate guide to RESTFul API Versioning in PHP Laravel: 2 solutions
Feb 15, 2023 programming

Ultimate guide to RESTFul API Versioning in PHP Laravel: 2 solutions

API versioning is what you need if you want to change the behavior of your API or the structure and format of requests and responses. ...

17 Min Read Read More
PHP range
Feb 15, 2023 programming

PHP range

Today we are going to talk about range function in PHP. It's a very simple yet very powerful tool and can save you a lot of time. it's also more readable than the alternatives. ...

3 Min Read Read More
PHP Array Combine
Feb 14, 2023 programming

PHP Array Combine

Today we are going to talk about array combine in PHP. Array combine is another useful array function that creates an array by using one array for keys and another for its values. ...

3 Min Read Read More
Alpine.js Beginner to Advanced 2025: Everything You Need to Know
Jul 18, 2025 programming

Alpine.js Beginner to Advanced 2025: Everything You Need to Know

JavaScript frameworks have exploded in popularity over the past decade, but not every project needs—or benefits from—the complexity of tools like React or Vue. Sometimes, all you want is to sprinkle interactivity into your HTML without a heavy build process. This is exactly where Alpine.js shines. ...

29 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 ...