Autowiring

Autowiring is an exotic word that represents something very simple: the ability of the container to automatically create and inject dependencies.

In order to achieve that, PHP-DI uses PHP's reflection to detect what parameters a constructor needs.

Let's take this example:

class UserRepository
{
    // ...
}

class UserRegistrationService
{
    public function __construct(UserRepository $repository)
    {
        // ...
    }
}

When PHP-DI needs to create the UserRegistrationService, it detects that the constructor takes a UserRepository object (using the type hinting).

Without any configuration, PHP-DI will create a UserRepository instance (if it wasn't already created) and pass it as a constructor parameter. The equivalent raw PHP code would be:

$repository = new UserRepository();
$service = new UserRegistrationService($repository);

As you can imagine, it's very simple, doesn't require any configuration, and it just works!

Configuration

Autowiring is enabled by default. You can disable it using the container builder:

$containerBuilder->useAutowiring(false);

Limitations

PHP-DI won't be able to resolve cases like this:

class Database
{
    public function __construct($dbHost, $dbPort)
    {
        // ...
    }

    public function setLogger(LoggerInterface $logger)
    {
        // ...
    }
}

For those classes, you will need to use PHP definitions to declare explicitly what to inject.