Possibly not. Using app->bind comes in handy if you have a tree of classes with dependencies on other classes.
For simple cases just use the standard constructor.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi I'm new to Laravel and I'm trying to understand the service container. In my client code I would normally write something like this:
$user = new User($data['username'], new EmailAddress($data['email'], etc etc);
But since User depends on EmailAddress I should use the service container, right?
According to the docs: "There is no need to bind classes into the container if they do not depend on any interfaces. The container does not need to be instructed on how to build these objects, since it can automatically resolve these objects using reflection."
I'm not using an interface, so from my understanding I should not need to bind.
$user = App::make(User::class);
But it gives me the error:
Unresolvable dependency resolving [Parameter #0 [ string $emailAddress ]] in class App\Domain\User\EmailAddress
And the only way I know how to fix it is with bindings:
$this->app->bind(User::class, function($app, array $parameters)
{
$emailAddress = new EmailAddress($parameters[1]);
return new User($parameters[0], $emailAddress, $parameters[2], $parameters[3]);
});
and
$user = App::make(User::class, $data['username'], $data['email'], etc etc);
Is this the right solution to the problem or is there a better way? Should I even use the service container here in the first place? I don't really see the point...
Possibly not. Using app->bind comes in handy if you have a tree of classes with dependencies on other classes.
For simple cases just use the standard constructor.
Please or to participate in this conversation.