SOLID Principles
S Single Responsibility

SOLID โ€” 01

Single Responsibility

One class โ€” one reason to change

โ†“ click to explore

SOLID โ€” 01 / Flow

S

Single Responsibility

A class should have one, and only one, reason to change.

UserManager
BAD โ€” validates + saves + emails + reports
โ†“ Split by SRP
UserValidator
OK โ€” validates only
UserRepository
OK โ€” saves only
EmailService
OK โ€” emails only
ReportGenerator
OK โ€” reports only
O Open/Closed

SOLID โ€” 02

Open/Closed

Open for extension, closed for modification

โ†“ click to explore

SOLID โ€” 02 / Flow

O

Open/Closed

Add new features without changing existing code.

Core Plugin
CLOSED โ€” never modified
do_action(“before_save”) โ€” Hook = Extension Point
Your Plugin
OK โ€” add_action() extends without touching core
Another Plugin
OK โ€” add_filter() extends independently
L Liskov Substitution

SOLID โ€” 03

Liskov Substitution

Subtypes must be substitutable for base types

โ†“ click to explore

SOLID โ€” 03 / Flow

L

Liskov Substitution

If B extends A โ€” B must work wherever A is expected.

Shape (interface)
getArea(): float
Rectangle implements Shape
OK โ€” width ร— height
Square implements Shape
OK โ€” sideยฒ
printArea(Shape $shape)
OK โ€” works with ANY Shape โ€” LSP
I Interface Segregation

SOLID โ€” 04

Interface Segregation

Many specific interfaces over one general

โ†“ click to explore

SOLID โ€” 04 / Flow

I

Interface Segregation

No client should be forced to depend on methods it does not use.

Worker (fat interface)
BAD โ€” work + eat + sleep โ€” Robot forced to implement all
โ†“ Split by ISP
Workable
OK โ€” work()
Feedable
OK โ€” eat()
Restable
OK โ€” sleep() + takeBreak()
D Dependency Inversion

SOLID โ€” 05

Dependency Inversion

Depend on abstractions, not concretions

โ†“ click to explore

SOLID โ€” 05 / Flow

D

Dependency Inversion

High-level modules should not depend on low-level modules.

UserService
depends on Database_Interface โ† abstraction
Database_Interface
save() / find() / delete()
MySQL_Database
OK โ€” implements interface
Mock_Database
OK โ€” implements interface โ€” for tests