🐘 PHP Advanced

What are PHP design patterns for solving common problems?

Answer

Common PHP design patterns: Singleton (one instance — use with caution, makes testing hard), Factory/Abstract Factory (encapsulate object creation), Builder (construct complex objects step-by-step), Repository (abstract data access), Observer/Event Dispatcher (publish-subscribe), Strategy (swap algorithms at runtime), Decorator (add behavior dynamically), Adapter (wrap incompatible interfaces), Facade (simplify a complex subsystem — Laravel's facades), Proxy (control access to an object), Command (encapsulate a request as an object — used in command buses), and Specification (encapsulate business rules as objects that can be combined). Modern PHP favors composition over inheritance and dependency injection over singletons.