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 Database
Mar 22, 2023 programming

PHP Database

Today we are going to talk about working with a database in PHP. working with databases is a very important part of web applications. you should be able to store data and read them. ...

14 Min Read Read More
PHP execution time
Feb 15, 2023 programming

PHP execution time

Today we are going to talk about calculating execution time in PHP. Sometimes it’s a good idea to calculate the execution time of your functions and script as a whole. ...

3 Min Read Read More
PHP Validate Email
Feb 15, 2023 programming

PHP Validate Email

Today we are going to talk about validating emails in PHP. In a lot of projects you need to validate the emails given by the user to make sure the email address has a correct format. ...

4 Min Read Read More
PHP Simple Web Crawler
Mar 22, 2023 programming

PHP Simple Web Crawler

Today we are going to build a simple web crawler in PHP and parse the box office movies from IMDB. We can do it with PHP easily. We don’t even need any extra package. ...

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