0

Resolution — what it means when a container 'resolves' a class

Beginner5 min read·eng-12-002
interviewlaravel-src

Definition

Resolution is the act of a dependency injection container looking up, constructing, and returning an instance of a class or interface. When you ask the container to "resolve" UserRepository, it reads its registered bindings, determines what concrete class fulfills that type, constructs it (recursively resolving its own dependencies), and returns the fully-assembled object. Resolution is not instantiation — it includes the entire traversal of the dependency graph.

In Practice

php
<?php

use Illuminate\Contracts\Container\Container;

// Binding: tell the container what to build when UserRepository is requested
app()->bind(UserRepository::class, EloquentUserRepository::class);

// Resolution: the container builds EloquentUserRepository,
// detecting its constructor dependencies and building those too
$repo = app()->make(UserRepository::class);

// Auto-resolution (no explicit binding needed for concrete classes):
// The container inspects the constructor via Reflection and resolves each
// type-hinted parameter recursively.
class OrderService
{
    public function __construct(
        private UserRepository $users,  // resolved automatically
        private MailService $mailer,    // resolved automatically
    ) {}
}

$service = app()->make(OrderService::class);
// $service->users is an EloquentUserRepository
// $service->mailer is a MailService (or whatever is bound)

// Manual resolution via type-hint in a controller method (route model binding
// and method injection use resolution internally):
Route::get('/orders', function (OrderService $service) {
    // Laravel resolves $service from the container before calling this closure
});

In Laravel, resolution happens when a controller is constructed, when app()->make() is called explicitly, or when you use resolve() or the App facade. The container caches singleton resolutions so the same instance is returned on subsequent requests within the same PHP process.

In Context

Interviewers use "resolution" to probe whether you understand that dependency injection is more than passing objects manually. The question might be "how does Laravel resolve a controller's dependencies?" — the expected answer walks through: the router detects type-hints in the constructor, calls the container's make() method for each, which triggers resolution of each dependency's own dependencies recursively.

A common misconception is equating resolution with new. The key difference is that the container is aware of the full dependency graph and can apply strategies (singleton, transient, scoped) at each node. new OrderService(new EloquentUserRepository(), new MailService()) is manual wiring; resolution is declarative wiring where the container does the work.

Related terms: "binding" is the registration step (telling the container what to build); "resolution" is the retrieval step. "Auto-wiring" refers to resolution without explicit bindings — the container uses PHP Reflection to discover constructor parameters and resolves them by type. Not all containers support auto-wiring; Laravel's container does.