Skip to content

Dependency injection into a union-typed parameter ​

When performing injection for a parameter with a union type, the container configuration is used to search for available PHP class declarations. If multiple logically equivalent options for dependency injection are found, an exception is thrown, indicating that the type of the argument being injected must be specified more precisely.

Below, we will look at an example of how to define an injection for a parameter with a union type.

PHP classes ​

php
<?php
declare(strict_types=1);

// file: /app/src/Services/Foo.php

namespace App\Services;

final class Foo
{ 
    public function __construct(
        public readonly Bar|Baz $dependency
    ) {}

}
php
<?php
declare(strict_types=1);

// file: /app/src/Services/Bar.php

namespace App\Services;

final class Bar
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Services/Baz.php

namespace App\Services;

final class Baz
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/container_build.php

require_once __DIR__.'/../vendor/autoload.php';

use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;

$container = (new DiContainerBuilder())
    ->build();

$foo = $container->get(Foo::class);

var_dump($foo->dependency);

Run the following command in the terminal:

shell
php /app/src/container_build.php

An exception will be thrown:

PHP Fatal error:

Uncaught Kaspi\DiContainer\Exception\AutowireParameterTypeException: Cannot automatically resolve dependency in App\Services\Foo::__construct(). Please specify the Parameter #0 [ <required> App\Services\Bar | App\Services\Baz $dependency ].

To resolve the error, you need to specify the argument type for the $dependency constructor parameter in the PHP class \App\Services\Foo.

Declaration in configuration files ​

In the services.php configuration file, using the diGet() helper function, we specify which dependency needs to be implemented in the $dependency constructor parameter of the \App\Services\Foo PHP class.

php
<?php
declare(strict_types=1);

// file: /app/src/container_build.php

require_once __DIR__.'/../vendor/autoload.php';

use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;

$container = (new DiContainerBuilder())
    ->build();

$foo = $container->get(Foo::class);

var_dump($foo->dependency);
php
<?php
declare(strict_types=1);

// file: /app/config/services.php

use App\Service\{Foo, Baz};

use function Kaspi\DiContainer\{diAutowire, diGet};

return static function (): Generator {
    yield diAutowire(Foo::class)
        ->bindArguments(
            dependency: diGet(Baz::class)
        );
};

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
  object(App\Services\Baz)#2 (0) {
  }

Declaration via an attribute ​

Using the \Kaspi\DiContainer\Attributes\Inject attribute, we specify exactly which dependency should be injected into the $dependency constructor parameter of the PHP class \App\Services\Foo.

php
<?php
declare(strict_types=1);

// file: /app/src/Services/Foo.php

namespace App\Services;

use Kaspi\DiContainer\Attributes\Inject;

final class Foo
{ 
    public function __construct(
        #[Inject(Baz::class)]
        public readonly Bar|Baz $dependency
    ) {}

}
php
<?php
declare(strict_types=1);

// file: /app/src/container_build.php

require_once __DIR__.'/../vendor/autoload.php';

use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;

$container = (new DiContainerBuilder())
    ->import(namespace: 'App\\', src: '/app/src')
    ->build();

$foo = $container->get(Foo::class);

var_dump($foo->dependency);

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
  object(App\Services\Baz)#2 (0) {
  }