SOLID Principles in PHP – Clean Code Architecture

Other Blogs

Blogs ❯❯ PHP

Image could not load

SOLID principles by Growtika on Unsplash

SOLID Principles क्या हैं ?

SOLID एक acronym है जो 5 Object Oriented Design principles को represent करता है, ये principles Robert C. Martin (Uncle Bob) ने introduce किये और modern software development में काफी important माने जाते हैं।

.

SOLID का full form :

  • S – Single Responsibility Principle (SRP)

  • O – Open/Closed Principle (OCP)

  • L – Liskov Substitution Principle (LSP)

  • I – Interface Segregation Principle (ISP)

  • D – Dependency Inversion Principle (DIP)

चलिए हर एक principle को PHP examples के through समझते हैं।

1. S - Single Responsibility Principle (SRP)

Principle : हर class या module की सिर्फ एक responsibility होना चाहिए।

अगर एक class multiple responsibilities handle करेगी—जैसे logging और report generation—todd complexity, testing में दिक्कत और bug के chances बढ़ते हैं।

class ReportGenerator { public function generateReport() { /* ... */ } } class ReportSaver { public function saveToFile(ReportGenerator $report, string $filePath) { /* ... */ } }

यहां ReportGenerator और ReportSaver अलग responsibilities handle करते हैं।

2. O – Open/Closed Principle (OCP)

Principle : Classes extension के लिए open होनी चाहिए, पर modification के लिए नहीं।

आप बिना existing code को touch किये new features add कर सके—मतलब Inheritance या Interfaces का use.

interface Shape { public function area(): float; } class Circle implements Shape { public function __construct(private float $radius) {} public function area(): float { return pi() * $this->radius ** 2; } } class Rectangle implements Shape { public function __construct(private float $width, private float $height) {} public function area(): float { return $this->width * $this->height; } }

new shapes add करने के लिए आप सिर्फ new classes बनाते हैं, existing classes को modify नहीं करते।

 3. L – Liskov Substitution Principle (LSP)

Principle : derived classes , base class के objects कि जगह stable तरीके से use हो सकती हों बिना correctness खोये। या ऐसा कह सकते हैं बिना base class के flow को disturb किये बिना।

अगर आप किसी method को parent class के reference से call करते हैं, तो child class को behave करना चाहिए expected way में।

class Bird { public function fly(): string { return "Flying"; } } class Sparrow extends Bird {} class Penguin extends Bird { public function fly(): string { throw new Exception("Penguin cannot fly."); } }

Penguin subclass LSP violate करता है, क्योंकि सब birds fly करते हैं—पर penguin नहीं।

4. I – Interface Segregation Principle (ISP)

Principle : Clients को unnecessary methods पर depend नहीं होना चाहिए।

एक ही interface में बहुत methods hone से users forced हो जाते उन methods में जो उन्हें use नहीं करते।

interface Printer { public function print(): void; } interface Scanner { public function scan(): void; } class MultiFunctionMachine implements Printer, Scanner { public function print(): void { /* ... */ } public function scan(): void { /* ... */ } }

यहां interface में defined methods focused हैं। printer-only clients scanner methods के साथ confuse नहीं होते।

5. D – Dependency Inversion Principle (DIP)

Principle : Higher-level modules को abstractions पर depend करना चाहिए, concrete implementations पर नहीं।

Class को directly concrete classes पर depend नहीं करना चाहिए—बल्कि interfaces / abstracts पर depend करें। इससे loosely-coupled और testable code बनेगा।

interface Notifier { public function send(string $message): void; } class EmailNotifier implements Notifier { public function send(string $message): void { // Email logic } } class UserController { public function __construct(private Notifier $notifier) {} public function notifyUser(): void { $this->notifier->send("Hello user!"); } }

अगर future में logging notifier add करना हो—बस new class बनाये , existing code change नहीं होता।

Benefits of SOLID Principles

  • Maintainability : Code modular और clear होता है, changes easier होते हैं।

  • Scalability : new functionalities add करना simple होता है with minimal risk . 

  • Readability & Testability : Small, focused classes tests और reviews में better perform करते हैं।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !