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

Single Responsibility Principle

general PHP 5.0+ Beginner

Also Known As

SRP Single Responsibility Principle one reason to change

TL;DR

A class or function should have one reason to change — doing one thing and doing it well.

Explanation

The S in SOLID, SRP states that every module, class, or function should be responsible for one part of the system's functionality. "One reason to change" means: if two different types of requirements changes would require editing the same class, that class probably has too many responsibilities. SRP makes code easier to understand, test, and modify because changes to one concern don't accidentally break another.

Common Misconception

SRP means a class should only do one thing. SRP means a class should have only one reason to change — one stakeholder or concern driving its evolution. A class with two unrelated responsibilities will be modified for two different reasons, coupling changes that should be independent.

Why It Matters

Classes with a single responsibility are easier to test in isolation, safer to change without side effects, and simpler to reuse. Most spaghetti codebases trace back to classes that grew to own too many concerns.

Common Mistakes

  • Confusing SRP with "one method per class" — it means one reason to change, not one action.
  • Putting database queries, business logic, and HTTP response building all inside a controller.
  • Splitting too aggressively — a 5-line helper class for every trivial operation creates its own maintenance burden.
  • Ignoring SRP during prototyping and never refactoring once the code "works".

Code Examples

✗ Vulnerable
class User {
    public function save(): void { /* DB logic */ }
    public function sendWelcomeEmail(): void { /* SMTP logic */ }
    public function toJson(): string { /* serialisation */ }
    public function validatePassword(string $pw): bool { /* validation */ }
    // Four reasons to change
}
✓ Fixed
class OrderController {
    public function store(Request $r, OrderService $orders) {
        $order = $orders->create($r->validated());
        return response()->json($order);
    }
}
// OrderService handles business logic, dispatches events/jobs

Added 13 Mar 2026
Edited 22 Mar 2026
Views 31
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 0 pings S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S 2 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 1 ping T 1 ping W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 1 ping W 0 pings T
No pings yet today
Perplexity 9 Amazonbot 6 Ahrefs 3 Google 3 ChatGPT 2 Unknown AI 2 SEMrush 2
crawler 25 crawler_json 2
DEV INTEL Tools & Severity
🟡 Medium ⚙ Fix effort: Medium
⚡ Quick Fix
If you struggle to name a class in one word (not 'Manager', 'Helper', 'Util'), it has multiple responsibilities — split it
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
Class named XxxManager XxxHelper XxxUtil with 10+ public methods across unrelated concerns
Auto-detectable: ✗ No phpstan phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: Medium False Positives: High ✗ Manual fix Fix: High Context: Class Tests: Update

✓ schema.org compliant