Explain the SOLID principles and give a verbal example of how Single Responsibility or Dependency Inversion applies to a PHP class.
Last updated
SOLID: Single Responsibility (one reason to change), Open/Closed (extend without modifying), Liskov Substitution (subclasses replace parents), Interface Segregation (small interfaces), Dependency Inversion (depend on abstractions).
SRP example: a
UserRegistrationclass that validates, saves, and emails should delegate email to a separateMailer.DIP example:
OrderServicedepends on an injectedPaymentGatewayinterface rather than creatingStripeGateway, so Stripe can be swapped for PayPal.