1

I am learning how Laravel's IoC container works. I already understand most of it, but one thing makes me think. Why are some bindings types starting with lowercase and others are uppercase?

I know that for uppercase ones, we can use automatic/manual resolver for classes names or interfaces. How about the lowercase ones? Are they meant to be resolved by hand only, whenever needed, like this?

$this->app->make('something');

Or is there any other purpose where lowercased bindings are used?

Robo Robok
  • 21,132
  • 17
  • 68
  • 126

1 Answers1

1

They are used as an abstract naming for the service, or an alias. For example if you have an http client configured for the one particular API service, you can name it, e.g. 'client.api', and resolve it dynamically in your code.

You can do it wherever you want via

app('client.api')

or

$this->app['client.api']

or even

$this->app->make('client.api');

It's just a key for the array of services.

Also they are widely used as resolvers in Facades.

Maxim Lanin
  • 4,351
  • 24
  • 32
  • I figured out that as an opposite to PHP's core behavior, container's class resolver is case sensitive. What's more, it's case sensitive comparing to the class name itself, not to the type hint. So if we don't follow PSR-2 convention and name our class in lowercase, it might be auto resolved by an alias. So am I right saying that there are only two ways to resolve from the container: automatic by parameters and manual by app('alias') and similar? – Robo Robok Jun 28 '15 at 21:41
  • Container is just an associative array, where keys are string names and values are services. Of course if you named your service 'SomeThing' and trying to fetch it as 'something', you'll get an error. So yes you have to resolve them right like you named them. And if you bind them like the fully class name, you will have an ability to autoresolve them. But if you named them abstractly, you will have to resolve them only manually. – Maxim Lanin Jun 28 '15 at 21:47
  • Thanks a lot for explanations! – Robo Robok Jun 28 '15 at 21:53
  • You are welcome! Don't forget to accept the answer ;) – Maxim Lanin Jun 28 '15 at 21:55