Skip to content
Antares

Autowiring

The container resolves classes and their entire dependency tree automatically. Concrete classes need no configuration.

use Antares\Container\Container;
class Logger {}
class UserService
{
public function __construct(
private readonly Logger $logger,
) {}
}
$container = new Container();
$service = $container->make(UserService::class);

Autowired classes are not shared: every make() call builds a new instance, and so does every class that depends on it. Register a class with singleton() or scoped() when it should be shared. See Bindings & Singletons.

The container walks the full constructor tree recursively:

class Cache {}
class Repository
{
public function __construct(
private readonly Database $db,
private readonly Cache $cache,
) {}
}
$repository = $container->make(Repository::class);

Both Database and Cache are resolved and injected automatically.

How Each Constructor Parameter Is Resolved

Section titled “How Each Constructor Parameter Is Resolved”
ParameterResolved as
Class or interface type (Database $db)$container->make(Database::class), always, even if it has a default value
Scalar or array type with a default (int $perPage = 15)The default value
No type, with a defaultThe default value
Scalar without a default (string $host)Error: register the class with singleton() and pass the value yourself
class Paginator
{
public function __construct(
private readonly Database $db, // resolved from the container
private readonly int $perPage = 15, // default used
) {}
}