PHP & Laravel devs.Have you ever thought how does the https://laravel.com/docs/9.x/facades static class call works with a method chaining. Let see a code example

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.

/** * 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); }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners