Support the ongoing development of Laravel.io →
posted 10 years ago
IOC

So I have a class which I'm using psr-4 to autoload.

I have access to it and I can call it's methods without having to instantiate anything (I assume this is due to reflection).

My question is this: If I have access to this class and it's methods why use app::bind() other than for dependancy injection?

Last updated 2 years ago.
0

Hello lucabernardino,

you use dependency injection not because you need to have access to you class but to make your code better organized. usually you bind you class to an interface.

app::bind('MyNamespace/PaymentInterface', function(){ return new PaypalPayment(); })

so in the future is you change a payment method the only change you have to do is this line of code.

a better and more extended explanation can be found on Taylor's book "Laravel: From Apprentice To Artisan"

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.