Interview question
What are Laravel Facades and how do they work internally? Laravel Facades क्या हैं और internally कैसे काम करते हैं?
Answer
Facades provide a static-looking interface to classes registered in the service container, giving expressive syntax without sacrificing testability.
// Using a facade
Cache::put('key', 'value', 600);
// What happens internally:
// 1. Cache::put() is intercepted by __callStatic()
// 2. Facade resolves the underlying 'cache' binding from the container
// 3. put() is called on that resolved instanceBehind the scenes:
Facade class → getFacadeAccessor() returns binding key → Service container resolves the real object → Method call is forwarded to it
Facade class → getFacadeAccessor() returns binding key → Service container resolves the real object → Method call is forwarded to it
| Aspect | Detail |
|---|---|
| Not truly static | Underlying class is a normal object resolved from container |
| Testable | Can be mocked via Cache::shouldReceive() |
Facades service container में registered classes के लिए static जैसा interface देते हैं, testability खोए बिना expressive syntax देते हैं।
Cache::put('key', 'value', 600);
// Internally:
// 1. __callStatic() intercept करता है
// 2. Facade container से असली binding resolve करता है
// 3. put() उस resolved instance पर call होता हैWas this answer clear?