🐘 PHP Intermediate

What is the Adapter pattern in PHP?

Why Interviewers Ask This

Mid-level PHP roles require deep understanding of this topic. Interviewers ask this to separate candidates who truly understand the mechanics from those who only know surface-level concepts.

Answer

The Adapter pattern allows incompatible interfaces to work together by wrapping one class with another that translates calls from the expected interface to the wrapped class's interface. It is a structural pattern that acts as a bridge between two incompatible interfaces. Example: you have third-party payment library StripePayment with a charge() method, but your application expects a PaymentGateway interface with a process() method. Create StripeAdapter implements PaymentGateway that wraps StripePayment and delegates process() to charge(). Now your application works with any gateway as long as you have an adapter. This is how PSR-7 adapters allow different frameworks to work with the same HTTP message format, and how logging adapters (Monolog) provide PSR-3 compliance for various logging backends.

Common Mistake

Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real PHP project.