Interface Binding in Laravel

Nifras Ismail
1 min readJul 11, 2019

--

Interface binding is a very useful part when your code is growing with more efficient.

Let’s see the use case of it. Then we get more clear on understanding.

Imagine!, we have an interface called SpacePostInterface 😉 now we are sending letters to the moon using the class SendLetter() method on MoonPost class which have its own implementation itself.

Our binding for the above classes and interface look likes below

$this->app->bind(SpacePostInterface::class, MoonPost::class);

after a few months, we decide to change our implementation of SendLetter() to using some faster technology — like sending email to the moon on the class MoonEmail class and bind that class to our SpacePostInterface.

So without changing any changes of usage of the SendLetter() method in our application we just want to change the binding to MoonEmail class will do the things elegance way in our app.

$this->app->bind(SpacePostInterface::class, MoonEmail::class);

Our code still works on the same way with the MoonEmail class, If the expected changes do not work well and we have a change of mind to the previous MoonPost class implementation we just change the class binding to the interface and use it in an elegance way. 👏

--

--

Nifras Ismail

Learning & Sharing is giving peace of mind on day to day life. It’s the end it is started …