Laravel Facades Quick Explanation

Laravel Facades are built on a combo of class aliasing and PHP meta-programming via the __callStatic() magic method.

Each Facade class has a single getFacadeAccessor() method that returns a string identifier used to retrieve the actual class underlying the Facade from Laravel's service container. So instead of having to interact with the service container like:

$app = app();
$app->make('cache')->has('cache_name');

one instead uses the facade:

Cache::has('cache_name');

The Cache facade class doesn't have a has() method, but the invocation gets passed to the parent Facade class' __callStatic method which proxies the call to the actual class serving as the cache provider (of which there can be many, ex. for Redis, filesystem, etc. written against the spec of the abstract cache class).

A great older series including a deep dive into Laravel's Facades from the time: https://alanstorm.com/laravel_facades/

Tags

 Laravel  Laravel Facades  PHP  meta-programming