Laravel Facades - How it works under the hood
Saravana Sai

Saravana Sai @saravanasai

About: I am a self taught Web developer interested in building something that makes people's life awesome. Writing Code For Humans Not For Dump Machines.

Location:
Trichy, Tamil Nadu, Indian
Joined:
Apr 24, 2022

Laravel Facades - How it works under the hood

Publish Date: Dec 31 '22
2 0

Hello ! PHP & Laravel devs.Have you ever thought how does the Laravel Facades static class call works with a method chaining. yeap I don't want to overwhelm . Let see a code example

Calculator::add(100)
    ->subract(50)
    ->add(30)
    ->add(10);

Enter fullscreen mode Exit fullscreen mode

In above code you can see a static method call Calculator class calling make function statically then have you ever wonder how the method chaining is working in a static function call.

How is this magic is happening under the hood of Laravel Framework . Let we start figure it out.

In Laravel they used the simple PHP Magic Functions to build that kind of behavior. Stop What is Magic Functions in PHP Magic Functions in PHP.

Definition :
Magic methods are special methods which override PHP's default's action when certain actions are performed on an object.

Laravel Facade using __call() & __callStatic() to implement this feature. Before see the code let understand how this both magic method works.

__call() Method:

__call() is triggered when invoking inaccessible methods in an object context.

__callStatic() Method:

__callStatic() is triggered when invoking inaccessible methods in a static context.

I know its lot of theory.Let see the implementation.

/**
 * Summary of Calculator
 * @var int $value
 * @static add()
 * @static subract()
 * @
 */
class Calculator extends hasFacade
{

    public int $value = 0;

    protected array $mappings = [
        'add' => 'addtion',
        'subract' => 'subtraction'
    ];

    /** 
     * Summary of add
     * @param int $no
     * @static add
     * @return Calculator
     */
    protected function addtion(int $no): self
    {

        $this->value += $no;

        return $this;

    }

    /**
     * Summary of subtract
     * @param int $no
     * @return Calculator
     */
    protected function subtraction(int $no): self
    {

        $this->value -= $no;

        return $this;

    }

    public function __call(string $name, array $arguments): self
    {
        $functionName = $this->mappings[$name];

        return $this->$functionName(...$arguments);

    }

    public static function __callStatic(string $method, array $arguments): self
    {

        $instance = (new static);

        $calledFuction = $instance->mappings[$method];

        return $instance->$calledFuction(...$arguments);

    }

}
Enter fullscreen mode Exit fullscreen mode

You can see the implementation and now you can use this Calculator class with static methods like this in below.

$finalValue = Calculator::add(100)
    ->subract(50)
    ->add(30)
    ->add(10);

var_dump($finalValue->value); // 90

Enter fullscreen mode Exit fullscreen mode

Explanation:

When the Calculator class invoked statically function call the add() method . The __callStatic() method get invoked & It make a new instance of Calculator class then it call the addtion() method using that instance by resolving and also return that instance . Then next method subraction() is invoked with that same instance . Then it call __call() method get invoked then it all the magic happens out. For re-usability you can move the both method to new class and extend it with the class you want the implementation.

Comments 0 total

    Add comment