How To Use Interface Segregation Principle in PHP/Laravel

Mohasin Hossain
4 min readMar 13, 2022
Interface Segregation Principle

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

The Interface Segregation Principle is the 4th principle of the SOLID principle. In SOLID I is stand for the Interface Segregation Principle. Interface Segregation Principle means A client should not be forced to implement an interface that it doesn’t use. or No client should be forced to depend on methods it doesn’t use. or a client should never depend on anything more than the method it’s calling.

Note: Replace the fat interface with many small, specific interfaces.

Let’s drive into deep

Let me give an example of how we violate the Interface Segregation Principle.

In the picture above you see a worker interface and it has two methods signature work() & sleep().

In the picture above you see a Human worker class implements the worker interface and implement the two methods work() & sleep(). it’s perfect as a human can work and sleep. Now take into the picture below.

In the picture above you see an Android worker class also implements the worker interface and implement the two methods work() & sleep(). At that point, we are breaking ISP(Interface Segregation Principle) as an android worker can work but not sleep. So as an android worker don’t sleep and still we are implementing the sleep() method in our Android worker class, here we are violating the ISP(Interface Segregation Principle. Though Android worker doesn’t need the sleep() method as it’s useless here but still we forced to implement the sleep() method here. Finally, we are using the interface here in the picture bellow.

So let’s fix the ISP(Interface Segregation Principle) violation.

To fix it we need a very specific interface, not any complex one. see the picture below on how we can fix the ISP violation.

In the picture above we extract our worker interface into two interfaces like workable interface and sleepable interface. And we create a new manageable interface. Now, look into our Human worker class below.

The human worker class now implements the workable, sleepable, and manageable interface. and this is absolutely perfect.

The Android worker class now implements a workable and manageable interface. and this is absolutely perfect. And now we don’t have to use a sleepable interface as the Android class doesn’t sleep.

And finally, the Captain class easily manage both the Human and Android class. And now no class violating the ISP anymore.

In conclusion, the Interface Segregation Principle is very simple to follow, it gives you the power to keep your interface simple, specific & clean. To follow the ISP you just need to be concerned about Is, if a class implements an interface, all of the methods of that interface must be useable in your class. if any method remains useless that means you are violating or breaking the ISP(Interface Segregation Principle).

Want to learn rest of the SOLID principals:

For more details of SOLID principle, 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