SOLID principles software development me use hote hain code ko maintainable, reusable aur scalable banane ke liye.

1️⃣ Single Responsibility Principle (SRP)

A class should have only one reason to change

// ❌ Bad Example
class User {
    public function saveUser() { /* save user */ }
    public function sendEmail() { /* send email */ }
}

// ✅ Good Example
class User { 
   public function saveUser() 
   { /* save user */ } 
}
class Email 
{ 
    public function sendEmail() 
    { /* send email */ } 
}

2️⃣ Open/Closed Principle (OCP)

Code should be open for extension but closed for modification.

// ❌ Bad Example
class Payment {
    public function pay($type) {
        if ($type == "credit") 
        { /* credit logic */ }
        elseif ($type == "paypal") 
        { /* paypal logic */ }
    }
}

// ✅ Good Example
interface PaymentMethod { 
    public function pay(); 
}
class CreditCard implements PaymentMethod 
{ 
     public function pay() 
     { /* credit logic */ } 
}
class PayPal implements PaymentMethod 
{ 
      public function pay() 
      { /* paypal logic */ } 
}

3️⃣ Liskov Substitution Principle (LSP)

Subclasses should be replaceable without affecting correctness.

// ❌ Bad Example
class Bird 
{ 
	public function fly() 
	{ /* logic */ } 
}
class Penguin extends Bird 
{
	
} // Penguins can't fly ❌

// ✅ Good Example
class Bird 
{
	
}
class FlyingBird extends Bird 
{ 
	public function fly() 
	{ /* logic */ } 
}
class Penguin extends Bird 
{} // No fly method

4️⃣ Interface Segregation Principle (ISP)

Don’t force classes to implement methods they don’t use.

// ❌ Bad Example
interface Worker {
    public function work();
    public function eat();
}
class Robot implements Worker 
{ 
	public function work() {} 
	public function eat() {} 
} // Robots don't eat ❌

// ✅ Good Example
interface Workable 
{ 
	public function work(); 
}
interface Eatable 
{ 
	public function eat(); 
}
class Human implements Workable, Eatable 
{ 
	public function work() {} 
	public function eat() {} 
}

5️⃣ Dependency Inversion Principle (DIP)

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

// ❌ Bad Example
class MySQLDatabase 
{ 
	public function connect() 
	{ /* connect logic */ } 
}
class App { 
	private $db;
    public function __construct() 
    { 
    	$this->db = new MySQLDatabase(); 
    } // Tight coupling ❌
}

// ✅ Good Example
interface Database { 
	public function connect(); 
}
class MySQLDatabase implements Database 
{ 
	public function connect() { /* connect logic */ } 
}
class App {
    private $db;
    public function __construct(Database $db) 
    { 
    	$this->db = $db; 
    } // Loose coupling ✅
}

Final Summary

Summary of SOLID Principles in PHP

PrincipleMeaning
S – Single ResponsibilityOne class = One responsibility
O – Open/ClosedExtend, don’t modify existing code
L – Liskov SubstitutionDerived classes must not break parent behavior
I – Interface SegregationSmall, specific interfaces over one big interface
D – Dependency InversionDepend on abstractions, not concrete classes