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 Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
3 Ways to add Conditional and Loop within HTML
Mar 21, 2023 programming

3 Ways to add Conditional and Loop within HTML

Today we are going to talk about different ways we can add PHP conditionals and loops within HTML . ...

9 Min Read Read More
PHP Pass Arguments by Reference
Jan 12, 2023 programming

PHP Pass Arguments by Reference

Today we are going to talk about splat passing arguments by reference in PHP. By default when we pass argument to a function we pass it by value. ...

6 Min Read Read More
TypeScript Beginner to Advanced 2025: The Complete Guide
Jul 18, 2025 programming

TypeScript Beginner to Advanced 2025: The Complete Guide

TypeScript is no longer optional for serious JavaScript developers, it’s essential. Whether you're building modern front-end applications with React, scaling backend services with Node.js, or contributing to large open-source projects, TypeScript is the industry standard in 2025. ...

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