Chaining methods from a Laravel  helper class

Chaining methods from a Laravel helper class

Method chaining is a technique where a class with multiple methods can have those methods chained together and called in sequence into a single command.

A benefit of this can be cleaner and less repetitive code.

As an example, I created a helper class with methods responsible for calculations. In this case, the calculations are super simple, but in reality, they can be as complex as required, for example, a series of steps needed in a tax calculation.

My numbers class

My numbers class offers the ability to complete add, multiply and minus calculations.

The class is used when a form posts data to a method that calls an instance of this class and contains a command using one or more of its methods - also repeating if required.

The class requires data to be passed to it in the form of an integer. This integer is defined as a private property, and a construct enables the property to be easily called in subsequent methods, which return the value of the calculation the respective method performed on the original property.

Here a setter and getter pattern is used. The __construct method is the setter setting the reference to the property, and the final get method is the getter, which returns the value of the $number property.

Since the methods responsible for calculations' return is static, this only returns a reference to the class. The get is required to then return the value of the property.

Both setter and getter are vital for the class to work.

<?php

namespace App\Http\Helpers;

class Numbers
{
    private int $number;

    public function __construct(int $number)
    {
        $this->number = $number;
    }

    public function addTen(): static
    {
        $this->number += 10;

        return $this;
    }

    public function multiplyTen(): static
    {
        $this->number *= 10;

        return $this;
    }

    public function minusTen(): static
    {
        $this->number -= 10;

        return $this;
    }

    public function get(): int
    {
        return $this->number;
    }
}

Calling my numbers class and chaining results

Using my helper class and chaining its methods works like this.

With the posted data, I create a new instance of the Numbers helper and pass is the posted number data as $number .

I then store the result of accessing the Numbers helper with whatever sequence of calculations I require - with repetition possible. So long as the chained command is terminated with a call to the getter by calling the get() method.

💡
Note that this does not appear to work with BODMAS order of operations.
public function store(): view
    {
        $number = new Numbers(request('number'));
        $data = $number
            ->multiplyTen()
            ->addTen()
            ->minusTen()
            ->addTen()
            ->get();

        return view('numbers.result', compact('data'));
    }

In conclusion, method chaining is a powerful feature that can make your code cleaner and more efficient and can be straightforward to implement.