Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

Method chaining in PHP in a nutshell

In this short article, I’m going to explain what method chaining is and how it can prove to be useful as an object oriented design pattern.


If you have ever worked with popular PHP frameworks, such as Laravel or CodeIgniter, you’ve surely come across the following pattern of writing APIs.

$user = DB::table('users')->where('name', 'John')->first();

As you can see in the above example, this is how you can retrieve a single row/column from a table in Laravel. Notice how the method names(where, first) are chained together to obtain the final result. This is a method for designing object-oriented APIs, called Fluent Interface.

Fluent Interfaces implements method chaining by returning the class instance from each method that needs to be chained. In PHP, we can achieve this by returning $this from the class methods. Let’s understand how method chaining is useful by first looking at the example without method chaining and then after with method chaining.

Without method chaining

<?php

class Book
{
    private string $name;
    private string $author;

    public function setName(string $name)
    {
        $this->name = $name;

        return $this->name;
    }

    public function setAuthor(string $author)
    {
        $this->author = $author;

        return $this->author;
    }

    public function __toString()
    {
        $bookInfo = 'Book Name: ' . $this->name . PHP_EOL;
        $bookInfo .= 'Book Author: ' . $this->author . PHP_EOL;

        return $bookInfo;
    }
}

$book = new Book();

$book->setName('Harry Potter');
$book->setAuthor('J. K. Rowlings');

echo $book;

// Book Name: Harry Potter
// Book Author: J. K. Rowlings

As you can see above, after setting the values to the class properties, we’re returning those itself. But here, while setting properties from the object instance, this unnecessarily increases the amount of code needed when interacting with a class or an instance of a class. Let’s see how we can optimize this by applying method chaining to the same example.

With method chaining

<?php

class Book
{
    private string $name;
    private string $author;

    public function setName(string $name)
    {
        $this->name = $name;

        return $this;
    }

    public function setAuthor(string $author)
    {
        $this->author = $author;

        return $this;
    }

    public function __toString()
    {
        $bookInfo = 'Book Name: ' . $this->name . PHP_EOL;
        $bookInfo .= 'Book Author: ' . $this->author . PHP_EOL;

        return $bookInfo;
    }
}

$book = new Book();

$book->setName('Harry Potter')->setAuthor('J. K. Rowlings');

echo $book;

// Book Name: Harry Potter
// Book Author: J. K. Rowlings

As you can see, by returning $this from the class methods after setting class properties, we’ve now achieved method chaining which can be used on the object instance, which not only reduces the amount code needed but also improved the readability of the code. It’s terse, yet allows you to put more into a single line elegantly. Method chaining also allows for DRY approach.

Another advantage of using method chaining is it’s a top-down approach with arguments placed next to the function unlike the nested calls, where tracking down respective function calls to its arguments is demanding.

As for the practical use of method chaining, you would find it in CodeIgniter’s Active Record use:

$this->db->select('something')->from('table')->where('id', $id);

The above looks much cleaner than

$this->db->select('something');
$this->db->from('table');
$this->db->where('id', $id);
Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?