Solid Principles PHP
..
The SOLID principles can also be applied to PHP to improve the design and maintainability of your code. Here's a breakdown of each principle with examples in PHP:
1. Single Responsibility Principle (SRP)
Definition: A class should have only one reason to change, meaning it should have only one job or responsibility. Example in PHP:
class Book {
private
$title;
private
$author;
public
function __construct($title, $author) {
$this->title =
$title;
$this->author =
$author;
}
public
function getTitle() {
return
$this->title;
}
public
function getAuthor() {
return
$this->author;
}
}
class BookPrinter {
public
function print(Book $book) {
echo
$book->
getTitle() .
" by " .
$book->
getAuthor();
}
}
In this example, the Book
class is responsible for storing book data, while the BookPrinter
class handles printing the book details, adhering to SRP.
2. Open/Closed Principle (OCP)
Definition: Software entities (classes, modules, functions, etc.) should be open for extension but closed for modification. Example in PHP:
abstract
class Shape {
abstract
public
function draw();
}
class Circle extends Shape {
public
function draw() {
echo
"Drawing a Circle";
}
}
class Square extends Shape {
public
function draw() {
echo
"Drawing a Square";
}
}
class Drawing {
public
function drawShape(Shape $shape) {
$shape->
draw();
}
}
In this example, new shapes can be added by extending the Shape
class without modifying the existing code.
3. Liskov Substitution Principle (LSP)
Definition: Objects of a superclass should be replaceable with objects of a subclass without affecting the correctness of the program. Example in PHP:
class Bird {
public
function fly() {
echo
"Flying";
}
}
class Sparrow extends Bird {
// Inherits fly() method
}
class Ostrich extends Bird {
// Ostrich cannot fly, so it violates LSP if fly() is called on an Ostrich instance
}
To adhere to LSP, you might need to redesign the hierarchy:
abstract
class Bird {
// No fly method
}
class FlyingBird extends Bird {
public
function fly() {
echo
"Flying";
}
}
class Sparrow extends FlyingBird {
// Inherits fly() method
}
class Ostrich extends Bird {
// Does not have fly() method
}
4. Interface Segregation Principle (ISP)
Definition: Clients should not be forced to depend on interfaces they do not use. Example in PHP:
interface Worker {
public
function work();
public
function eat();
}
class WorkerImpl implements Worker {
public
function work() {
echo
"Working";
}
public
function eat() {
echo
"Eating";
}
}
class Robot implements Worker {
public
function work() {
echo
"Working";
}
public
function eat() {
// Robots do not eat, violates ISP
}
}
To adhere to ISP:
interface Workable {
public
function work();
}
interface Eatable {
public
function eat();
}
class HumanWorker implements Workable, Eatable {
public
function work() {
echo
"Working";
}
public
function eat() {
echo
"Eating";
}
}
class Robot implements Workable {
public
function work() {
echo
"Working";
}
}
5. Dependency Inversion Principle (DIP)
Definition: High-level modules should not depend on low-level modules. Both should depend on abstractions (e.g., interfaces). Abstractions should not depend on details. Details should depend on abstractions. Example in PHP:
class Light {
public
function turnOn() {
echo
"Light is on";
}
public
function turnOff() {
echo
"Light is off";
}
}
class Switch {
private
$light;
public
function __construct(Light $light) {
$this->light =
$light;
}
public
function operate() {
$this->light->
turnOn();
}
}
To adhere to DIP:
interface Switchable {
public
function turnOn();
public
function turnOff();
}
class Light implements Switchable {
public
function turnOn() {
echo
"Light is on";
}
public
function turnOff() {
echo
"Light is off";
}
}
class Switch {
private
$device;
public
function __construct(Switchable $device) {
$this->device =
$device;
}
public
function operate() {
$this->device->
turnOn();
}
}
By applying these principles in PHP, you can create a system that is easier to maintain and extend, leading to better code quality and more robust applications.
Leave a comment