SOLID Principles in PHP Explained with Examples
The SOLID principles are a set of design principles that help to maintain an object-oriented code introduced by Robert C.Martin. This post explains each of these principles separately.

SOLID principles are five objects or principles introduced by Robert C.Martin that help developers to write clean, maintainable and scalable code. An application following these principles is easier to extend, test and maintain. Code following SOLID principles is modular which makes unit testing easier.
SOLID Principles in Object Oriented PHP Programming
Like many other programming languages SOLID principles are widely used in PHP Object Oriented Programming as PHP design patterns to ensure the application code is organized and easily maintainable. Let's understand each of these principles with basic examples.
S - Single Responsibility Principle (SRP)
This principle states that a class should have only one job or responsibility to do. For example a user class that handles authentication and storing user data will violate this principle. Instead there should be two separate classes for each job. The Single Responsibility Principle example is as below:
class UserAuthentication
{
public function authenticateUser()
{
// Logic for authenticating the user
}
}
class UserDataStorage
{
public function saveUserData()
{
// Logic for saving user data
}
}
O - Open/Closed Principle (OCP)
This principle states that it should be possible to extend classes, modules, function or methods but they should not be allowed to modifications or changes. Like a class should be extended for additional functionality rather than changing the original code of class. The Open/Closed Principle example is as below where Shape class is open for extension but closed for modification:
// Main class
abstract class Shape
{
abstract public function calculateArea();
}
// Extended class
class Rectangle extends Shape
{
public $width;
public $height;
public function calculateArea()
{
return $this->width * $this->height;
}
}
// Extended class
class Circle extends Shape
{
public $radius;
public function calculateArea()
{
return pi() * ($this->radius * $this->radius);
}
}
L - Liskov Substitution Principle (LSP)
This principle outlines that the objects of a superclass should be replaceable with objects of subclass but it should not affect the correctness of code. For example if a subclass overrides methods of its superclass it should be in a way that clients can interact with subclass as an instance of superclass. The Liskov Substitution Principle example is as below:
// Interface for birds that can fly
interface CanFly
{
public function fly();
}
class Bird
{
// General behavior common to all birds
public function eat()
{
// Logic for eating
}
}
// Class for birds that can fly
class Sparrow extends Bird implements CanFly
{
public function fly()
{
// Logic for flying
}
}
// Class for birds that can't fly
class Ostrich extends Bird
{
public function run()
{
// Logic for running
}
}
I - Interface Segregation Principle (ISP)
This principles states that clients should not be forced to depend on interfaces they do not use. A good practice is to have smaller and more specific interfaces than one large interface with many methods. This way clients can implement the interfaces according to their needs. The Interface Segregation Principle example is as below:
interface Animal
{
public function eat();
}
interface FlyingAnimal
{
public function fly();
}
class Bird implements Animal, FlyingAnimal
{
public function eat()
{
// Eating logic
}
public function fly()
{
// Flying logic
}
}
class Fish implements Animal
{
public function eat()
{
// Eating logic
}
}
D - Dependency Inversion Principle (DIP)
This principle states that high-level modules should not depend on low-level modules. Instead both should depend on abstractions (e.g interfaces). Instead of a class directly creating its dependencies it should inject them via constructors or setter methods allowing class to be more flexible from specific implementations. The Dependency Inversion Principle example is as below:
interface Database
{
public function connect();
}
class MySQLDatabase implements Database
{
public function connect()
{
// MySQL connection logic
}
}
class Application
{
private $database;
public function __construct(Database $database)
{
$this->database = $database;
}
public function run()
{
$this->database->connect();
}
}
// Usage
$mysqlDB = new MySQLDatabase();
$app = new Application($mysqlDB);
$app->run();
We understood the SOLID principles in PHP with examples. These principles are fundamental for writing clean PHP code which is maintainable and scalable. Implementing SOLID principles in PHP application can significantly improve the quality of application.