SOLID principles in PHP

There are 5 principles in SOLID:

  • S – Single Responsibility
  • O – Open-Closed
  • L – Liskov Substitution
  • I – Interface Segregation
  • D – Dependency Inversion

This principles makes code clean, extensible and readable. If follow this principle you get architecture that ease to support.

Let’s detail this principles.

Single Responsibility Principle

A class should have one, and only one, reason to change.

It’s very straight forward and easiest one to follow.

The Single Responsibility Principle (SRP) just asks us to do tasks in separate Classes, instead of doing everything at one place (single Class). 

Here is the violation of this principle:

Here is refactored code: