DEV Community

Cover image for The 5 SOLID principles with examples [in PHP]
anastasionico
anastasionico

Posted on

The 5 SOLID principles with examples [in PHP]

Last week one of the account managers in the company I work for asked me to edit a little feature we have on one of the websites we manage,

Turn out I spent almost a week on that task.

If that happened to you, this is the time to change!

This website is very old and it is not actively managed, it was developed a long time ago then passed from developer to developer.

I must say, I work on it only once or twice a year.

The task itself was really easy and should have been a quick fix…

The reality?

It took 2 days only to understand what I was looking at.

Why do we like messes?

I realized that this is a common problem while deciding to refactor some code.

I really did not enjoy spending my time there, taking note of which interface was doing what, where a class was invoked and why there were methods 200 lines long.

As web developers, we are creators we want to see stuff on a web page that was blank only a few minutes before.

We must do in conscientiously though!

The way to write good code

Software engineers have been there for years and some of them have developed theories and principles that have been proved right times and times again.

One of the most famous and engineer that has done it is Robert Martin,
he has created a set of 5 principles that, if followed, will make every program scalable and most important incredibly easy to read.

That is called S.O.L.I.D.

What can you do?
It’s time to stop wasting days try to understand code written ages ago.
You can do it, and it only requires to follow 5 principles.

Read the full article here

Top comments (2)

Collapse
 
exadra37 profile image
Paulo Renato

I feel your pain, but I was there every month, sometimes every week, thus I end up with the Resourde Design Pattern, that @bmtich baptized by the Resource Action Pattern, and to be honest I think its a better name ;)

This pattern naturally makes easier for you to follow SOLID principles, while at same time it acts as documentation for your project, and without lies, because the folder structure is organized by all resources and actions you have in your project, instead of the traditional approach of by type of file, like Controller, Model, etc.

For a PHP example you visit this repo, and in the README of it you can see:

Anyone looking into the Resources folder can immediately see what Resources and what actions are available for each one
without the need to consult any documentation. This is the skeleton acting as documentation in our projects.
src/Resources/
├── Carts
│   ├── New
│   ├── Modify
│   ├── View
│   └── Remove
├── Categories
│   ├── New
│   ├── Modify
│   ├── View
│   └── Remove
├── Checkout
│   ├── New
│   ├── Modify
│   ├── View
│   └── Remove
├── Clients
│   ├── New
│   ├── Modify
│   ├── View
│   └── Remove
├── Products
│   ├── New
│   ├── Modify
│   ├── View
│   └── Remove
└── ...
The above skeleton by design will easily allow our code to follow the S.O.L.I.D Principles.

This approach makes it easier for a newbie or an experience developer to find the bit of code it needs to work in to fix a bug or improve a feature. To add a new feature it means usually a new resource, thus a new top level folder will need to be created.

So no more mess pilling up.

With this approach is easy to achieve PHP clases around 200-300 lines of code, and everything is isolated, thus also reduces merge conflicts in a team of developers.

Collapse
 
evrtrabajo profile image
Emmanuel Valverde Ramos

Hi 👋, a few days ago I posted an article where you talked about SOLID principles in PHP and how design patterns can help SOLID principles.

dev.to/evrtrabajo/solid-in-php-d8e