Intermediate LevelLaravel
How do custom Facades work under the hood via getFacadeAccessor()?
❓ Interview Prompt: Explain Facades architecture, static proxying, Service Container resolution, and getFacadeAccessor() method.
Facades provide a static syntax interface to classes registered in the Service Container.
How it Works Under the Hood:
1
Custom Facade extends 'IlluminateSupportFacadesFacade'.
2
Implements 'getFacadeAccessor()', returning the container binding string (e.g. 'payment-gateway').
3
Magic '__callStatic()' intercepts static calls, resolving the instance from the container and executing the method dynamically!
Code Implementation Example
php
<?php
namespace AppFacades;
use IlluminateSupportFacadesFacade;
class Payment extends Facade
{
protected static function getFacadeAccessor(): string {
return 'payment-gateway'; // Container binding key
}
}
// Usage: Payment::charge(100);💡Custom Facade implementation returning container service binding accessor key.
🎯 Interview Key Takeaway
Facades act as static proxies to classes bound in the Service Container via getFacadeAccessor().
#Facades#Service Container#Design Patterns#Intermediate
Back to all Laravel Questions→