Explain the 'Facade' pattern in PHP. What are the trade-offs between using Facades versus Dependency Injection?
Last updated
A Facade gives a static-looking interface (e.g.
Cache::get()) that actually resolves a real object from the service container viagetFacadeAccessor()and__callStatic().Facades are concise and still mockable (
shouldReceive()), but they hide dependencies from the class signature. Dependency Injection makes them explicit in the constructor.Rule of thumb: prefer DI for core domain and service classes. Use Facades in glue code and controllers.
- https://laravel.com/framework/docs/12.x/facades
- https://medium.com/@prevailexcellent/laravel-facades-vs-dependency-injection-whats-the-difference-and-when-to-use-each-539be13808a9
- https://dev.to/anashussain284/laravel-facades-explained-why-they-feel-so-clean-and-powerful-1ldj
- https://laravel-docs.readthedocs.io/en/latest/facades/
- https://www.reddit.com/r/PHP/comments/tjdkos/is_there_any_advantage_of_laravel_facades_over/
- https://medium.com/@osmanaras50/demystifying-laravel-facades-a-clean-guide-for-developers-0b78884b95f4
- https://www.reddit.com/r/PHP/comments/bu1dqz/replacing_your_facades_with_dependency_injection/
- https://laravel.com/framework/docs/10.x/facades