What is the Proxy pattern in PHP?
Why Interviewers Ask This
Advanced questions like this reveal whether a candidate has internalized PHP deeply enough to make architectural decisions. Strong answers demonstrate both breadth and depth of experience.
Answer
The Proxy pattern provides a surrogate or placeholder object that controls access to another object. The proxy implements the same interface as the real subject and delegates work to it, while adding its own behavior. Types: Virtual Proxy (lazy initialization — creates the expensive object only when first needed), Protection Proxy (access control — checks permissions before forwarding), Remote Proxy (represents an object in a different address space), and Caching Proxy (caches results of expensive operations). PHP example: a LazyUserRepository proxy that only loads the database connection the first time a query is made. Laravel's Eloquent lazy loading relationships is a form of virtual proxy — related models are loaded only when accessed. Doctrine ORM uses proxy classes extensively for lazy-loading entities.
Common Mistake
Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong PHP candidates.
Previous
What is PHP FFI (Foreign Function Interface)?
Next
What is immutability and value objects in PHP?