← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

SOLID Principles (Overview)

quality PHP 5.0+ Intermediate

Also Known As

SOLID principles object-oriented principles OOP principles

TL;DR

Five object-oriented design principles — SRP, OCP, LSP, ISP, DIP — that together guide towards maintainable, extensible code.

Explanation

SOLID is an acronym for five related OO design principles: Single Responsibility (a class has one reason to change), Open/Closed (open for extension, closed for modification), Liskov Substitution (subclasses substitute for base classes), Interface Segregation (many specific interfaces over one general one), and Dependency Inversion (depend on abstractions, not concretions). Applied together they produce loosely coupled, highly cohesive code that is easy to test and extend. They are guidelines, not rules — apply them where they add value.

Common Misconception

SOLID is a strict ruleset to follow literally. It's a set of guidelines for managing change — over-applying them (e.g. creating an interface for every class) produces unnecessary complexity in small codebases.

Why It Matters

SOLID principles prevent the most expensive class of software problems — code that cannot be changed without cascading breakage. Teams that follow SOLID spend far less time debugging regressions and far more time shipping features.

Common Mistakes

  • Treating SOLID as dogma and applying all five principles to simple utility functions — they target complex, change-prone code.
  • Confusing Interface Segregation with having one method per interface — it means interfaces should match client needs, not be maximally small.
  • Implementing Open/Closed by wrapping everything in strategy objects before there is a real extension need.
  • Naming classes *Service or *Manager and calling it SRP — naming does not guarantee single responsibility.

Avoid When

  • Small scripts and one-off utilities — applying all five principles adds abstraction overhead with no maintenance benefit.
  • Applying SOLID dogmatically — creating interfaces for classes that will never have a second implementation is over-engineering.
  • Using SOLID as a checklist rather than guidelines — understanding the problem each principle solves matters more than compliance.

When To Use

  • Long-lived codebases that will be extended by multiple developers over time.
  • Any class that is becoming hard to test — testability problems usually signal a SOLID violation.
  • When adding a new feature requires modifying many existing classes — open-closed principle applies.
  • Library and framework code where consumers need to extend behaviour without modifying your source.

Code Examples

✗ Vulnerable
class Report {
    public function generate() { /* logic */ }
    public function saveToDatabase() { /* DB */ }
    public function exportToPdf() { /* PDF */ }
    public function sendByEmail() { /* mail */ }
    // Violates SRP, OCP, and DIP simultaneously
}
✓ Fixed
class Report {
    public function generate(): ReportData { /* pure logic */ }
}
class ReportExporter {
    public function __construct(private ExporterInterface $exporter) {}
    public function export(ReportData $data): void {
        $this->exporter->export($data);
    }
}

Added 13 Mar 2026
Edited 25 Mar 2026
Views 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 0 pings S 1 ping M 0 pings T 0 pings W 3 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Perplexity 8 Amazonbot 6 Google 5 Unknown AI 3 ChatGPT 3 Ahrefs 2
crawler 24 crawler_json 2 pre-tracking 1
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: High
⚡ Quick Fix
When a class is hard to test, it usually violates SRP or DIP — extract dependencies into interfaces and inject them through the constructor
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Class with new SomeClass() inside methods; class handling both business logic and persistence; 500+ line class
Auto-detectable: ✗ No phpstan psalm phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: High Context: File Tests: Update

✓ schema.org compliant