Skip to content

Dependency injection into variable-length parameters ​

When injecting dependencies into a variable length parameter, you can use combinations of helper functions or PHP attributes. The type checking [1] of the injected dependency occurs at runtime - at the time of execution, the container does not perform type validation when building the container.

WARNING

A variable-length parameter is optional. If no argument is provided, it will be omitted during dependency injection.

Let’s look at an example of declaring the inclusion of a variable-length argument.

PHP classes ​

php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Interfaces\RuleInterface;

final class Rules
{
    /** @var RuleInterface[] */
    public readonly array $rules;

    public function __construct(
        RuleInterface ...$inputRule
    ) {
        $this->rules = $inputRule;
    }
}
php
<?php
declare(strict_types=1);

// file: /app/src/Interfaces/RuleInterface.php

namespace App\Interfaces;

interface RuleInterface
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleA.php

namespace App\Rules;

use App\Interfaces\RuleInterface;

class RuleA implements RuleInterface
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleB.php

namespace App\Rules;

use App\Interfaces\RuleInterface;

class RuleB implements RuleInterface
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/Rules/RuleC.php

namespace App\Rules;

use App\Interfaces\RuleInterface;

class RuleC implements RuleInterface
{
    // …
}

It is necessary to configure the injection of dependencies into the variadic $inputRule constructor parameter of the PHP class \App\Services\Rules. The PHP classes should be injected in the following order:

  • \App\Rules\RuleB
  • \App\Rules\RuleA
  • \App\Rules\RuleC

The container provides multiple ways to declare the injection of a PHP class into a parameter; for this example, we will use either the helper function diGet() or the attribute \Kaspi\DiContainer\Attributes\Inject, depending on the method used to declare the dependency injection.


Declaration in configuration files ​

In the services.php configuration file, register the PHP class \App\Services\Rules using the diAutowire() helper function, and inject dependencies into the $inputRule parameter via the bindArguments() method.

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\Rules;

$container = (new DiContainerBuilder())
    ->load('/app/config/services.php')
    ->build();

$rules = $container->get(Rules::class);
var_dump($rules->rules);
php
<?php
declare(strict_types=1);

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

use App\Services\Rules;
use App\Rules\{RuleC, RuleB, RuleA};

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    yield 'services.rule_c' => diAutowire(RuleC::class);
    
    // many lines

    yield diAutowire(Rules::class)
        ->bindArguments(
            diGet(RuleB::class),
            diGet(RuleA::class),
            diGet('services.rule_c'), // reference to a container identifier
        );
};

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
array(3) {
  [0]=>
  object(App\Rules\RuleB)#26 (0) {
  }
  [1]=>
  object(App\Rules\RuleA)#28 (0) {
  }
  [2]=>
  object(App\Rules\RuleC)#21 (0) {
  }
}

The order of objects injected via the constructor into the \App\Services\Rules::$rules property is determined by the order in which the injections are declared in the services.php configuration file.

Именованные аргументы ​

Suppose you need to inject PHP classes into the \App\Services\Rules::$rules property, using string values as keys for the array. To achieve this, you can use named arguments when defining the injection for the $inputRule constructor parameter within the services.php configuration file.

When defining an injection using named arguments for variadic parameters, the rules outlined in the PHP documentation apply.

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\Rules;

$container = (new DiContainerBuilder())
    ->load('/app/config/services.php')
    ->build();

$rules = $container->get(Rules::class);
var_dump($rules->rules);
php
<?php
declare(strict_types=1);

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

use App\Services\Rules;
use App\Rules\{RuleB, RuleA};

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    yield diAutowire(Rules::class)
        ->bindArguments(
            inputRule_B: diGet(RuleB::class),
            inputRule_A: diGet(RuleA::class),
        );
};

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
array(2) {
  ["inputRule_B"]=>
  object(App\Rules\RuleB)#26 (0) {
  }
  ["inputRule_A"]=>
  object(App\Rules\RuleA)#28 (0) {
  }
}

When using string keys in the \App\Services\Rules::$rules property, you can access injected classes by specifying the argument name:

php
$rules = $container->get(Rules::class);

$ruleA = $rules->rules['inputRule_A'];
$ruleB = $rules->rules['inputRule_B'];

TIP

For implementing collections, it is preferable to use tags.


Declaration via an attribute ​

Let’s declare the injection of a dependency into the $inputRule constructor parameter of the PHP class \App\Services\Rules using the \Kaspi\DiContainer\Attributes\Inject attribute.

php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Interfaces\RuleInterface;
use Kaspi\DiContainer\Attributes\Inject;
use App\Rules\{RuleB, RuleA, RuleC};

final class Rules
{
    /** @var RuleInterface[] */
    public readonly array $rules;

    public function __construct(
        #[Inject(RuleB::class)]
        #[Inject(RuleA::class)]
        #[Inject(RuleC::class)]
        RuleInterface ...$inputRule
    ) {
        $this->rules = $inputRule;
    }
}
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\Rules;

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

$rules = $container->get(Rules::class);
var_dump($rules->rules);

Run the following command in the terminal:

shell
php /app/src/container_build.php

A message like the following will appear:

console
array(3) {
  [0]=>
  object(App\Rules\RuleB)#26 (0) {
  }
  [1]=>
  object(App\Rules\RuleA)#28 (0) {
  }
  [2]=>
  object(App\Rules\RuleC)#21 (0) {
  }
}

The order of objects embedded via a constructor into the \App\Services\Rules::$rules property is determined by the order of declaration of injection via an attribute.


  1. Type hints – the specification of an expected data type for method parameters, such as the name of a specific class or interface. ↩︎