Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pmall's avatar

Why pass $app into your closure if it is not a dependency? Why use the 'use' keyword to make $param visible to the closure?

This is just examples to show you how it can be used. Stop overthinking :p

For the param, see @squigg post above. and there is nothing wrong with using use.

sorcjc's avatar

Hi. Sorry for post in this old thread. But my question is related to the same.

As I understand, Service Providers are useful because:

  • they allow us creating new instances than require some configuration
  • we can use a contract (or interface) and change the implementation of the service if needed

A service provider is a class that can register bindings in the service container, and provide services in this way.

However, if I have a service with no dependencies (a simple class instantiation), I don't need service providers. Is it?

I have a class CvHandler that I want to use from different locations. So I was creating a service provider, to be able to use dependency injection.

However, I have not added $this->app->bind(CvHandler::class); to the register method, and the dependency injection is available for this class (it means it is not necessary to use providers for classes with no dependencies, right?).

Edit: It seems that is not necessary to use providers. But how Laravel uses reflection? (What class contains the implementation?)

kewlashu's avatar

I reached to this discussion via googling for "Laravel Service Provider Example". Though the discussion clarified a lot of things for me but reading this article made it complete for me. Hopefully it will enrich the discussion.

1 like
Previous

Please or to participate in this conversation.