How To Use Dependency Inversion Principles in PHP/Laravel

Mohasin Hossain
4 min readMar 26, 2022

In this tutorial, I’ll show you in a very simple way with the example, of how you can use DIP(Dependency Inversion Principle) in PHP/Laravel. let’s start walking…

The Dependency Inversion Principle is the 5th and last principle of the SOLID principle. In SOLID D is stand for the Dependency Inversion Principle.

Note: Dependency Inversion is not equal to Dependency Injection.

The Dependency Inversion Principle means depends on abstraction, not concretion. high-level modules should not depend on low-level modules. Both should depend on abstraction never depend on anything concrete, only depend on abstraction. Low-level classes implement basic operations such as working with a disk, transferring data over a network, connecting to a database, etc. High-level classes contain complex business logic that directs low-level classes to do something. All of this is about decoupling code and IOC (inversion of control). High-level code isn’t as concerned with details, low-level code is more concerned with details & specifics.

Before diving into deep, I am going to give you a live example, imagine your mobile phone need to be charged, does your mobile phone need to know about how your electricity connection works behind the socket? no, your mobile phone just needs an interface like a socket to plug in. This is what Dependency Inversion we need to understand.

Let’s drive into deep

Let me give an example of how the Dependency Inversion Principle works.

In the picture above, we have a PasswordReminder class that depends on the Dbconnection class. Here we violate the Dependency Inversion principle. Does PasswordReminder class need to know how the database connection is working? no, it just needs to know there is a way to connect that’s it. It should depend on abstraction, not any concrete implementation. Here PasswordReminder class is a high-level module, it should not depend on low-level modules like database connection. To solve the violation we need an interface like the picture below.

Now our PasswordReminder depends on abstraction not any concrete implementation like the picture below. It’s no longer concern about which database connection we have and how it works. it’s just known there is a way to connect to the database.

Here Dbconnection is a low-level module. Now our low-level module should depend on abstraction too like the picture below.

Now our high-level module which is PasswordReminder does not depend on the low-level module which is Dbconnection it depends on abstraction and the low-level module Dbconnection also depends on abstraction, both modules depend on abstraction, not any concrete implementation.

In conclusion, the Dependency Inversion Principle is very simple to follow, it gives you the ability to change an implementation easily without altering the high-level code. To follow the DIP(Dependency Inversion Principle) you just need to be concerned about Is, high-level modules should not depend on low-level modules. Both should depend on abstraction, not concretion.

And finally, code to an interface always helps to less violate the SOLID principles.

Want to learn rest of the SOLID principals:

Want to learn rest of the SOLID principals:

For more details of SOLID principles, you may have a look at the link below. Please feel free to leave a comment if you have any feedback, questions, or want me to write about another PHP/Laravel related topic.

https://github.com/mohasinhossain/SOLID-Principles-in-PHP

--

--

Mohasin Hossain

Senior Software Engineer | Mentor @ADPList | Backend focused | PHP, JavaScript, Laravel, Vue.js, Nuxt.js, MySQL, TDD, CI/CD, Docker, Linux