Skip to content

Declaring dependency injection via container parameters ​

Injecting container parameters into the arguments of a method or a callable type[1]:

Both the helper function and the attribute accept a single argument, $name, which represents the name of the container parameter.

TIP

If an empty string is passed to the $name parameter, the container will attempt to automatically determine the container parameter name based on the name of the method (function) parameter or the named argument.

Example ​

PHP classes:

php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Enums\SetupEnum;

final class Foo
{
    public function __construct(
        public readonly Bar $bar,
        public readonly SetupEnum $setupType,
        public readonly string $mailTo,
        public readonly bool $debug,
    ) {}
}
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/Enums/SetupEnum.php

namespace App\Enums;

enum SetupEnum
{
    case LOCAL;
    case STAGES;
    case PROD;
}

Container parameter configuration files:

php
<?php
declare(strict_types=1);

// file: /app/config/params.php

use App\Enums\SetupEnum;

return [
    'setupType' => SetupEnum::PROD,
    'emails.admin' => 'admin@example.com',
    'emails.manager' => 'manager@example.com',
    'app.debug' => false,
];
php
<?php
declare(strict_types=1);

// file: /app/config/params_dev.php

use App\Enums\SetupEnum;

return [
    'setupType' => SetupEnum::LOCAL,
    'emails.admin' => 'admin@localhost',
    'emails.manager' => 'manager@localhost',
    'app.debug' => true,
];
php
<?php
declare(strict_types=1);

// file: /app/config/params_notify.php

return [
    'emails.notify' => '{emails.admin}, {emails.manager}',
];

IMPORTANT

The order in which container parameters are declared matters! If a container parameter has already been declared and another parameter with the same name is subsequently declared, its value will be overwritten the value from the latest declaration will be used.

Inject values into the constructor of the PHP class \App\Services\Foo via container parameters.

The container parameters must be defined according to the following condition: if the APP_ENV environment variable is set to 'dev', the container parameters should be loaded from the params_dev.php file; in all other cases, they should be loaded from the params.php file.

Declaration in configuration files ​

In the services.php configuration file, define the PHP class \App\Services\Foo and configure the injection of container parameters into its constructor using the diParameter() helper function:

  • 'setupType' into the $setupType constructor parameter.
  • 'emails.notify' into the $mailTo constructor parameter.
  • 'app.debug' into the $debug constructor parameter.
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;

use function {getenv, putenv};

putenv('APP_ENV=dev');

$builder = (new DiContainerBuilder())
    ->loadParameters('/app/config/params.php')
    ->loadParameters('/app/config/params_notify.php')
    ->load('/app/config/services.php');

if ('dev' === getenv('APP_ENV')) {
    $builder->loadParameters('/app/config/params_dev.php');
}

$container = $builder->build();

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

var_dump($foo->bar);
var_dump($foo->setupType);
var_dump($foo->mailTo);
var_dump($foo->debug);
php
<?php
declare(strict_types=1);

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

use App\Services\Foo;

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

return static function (): Generator {
    yield diAutowire(Foo::class)
        ->bindArguments(
            /*
             * Parameter #2 of the constructor, named `$setupType`.
             * Equivalent to the declaration diParameter('setupType')
             */
            setupType: diParameter(),
            mailTo: diParameter('emails.notify'),
            debug: diParameter('app.debug'),
        );
};

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\Bar)#2 (0) {
}
enum(App\Enums\SetupEnum::LOCAL)
string(33) "admin@localhost, manager@localhost"
bool(true)

Declaration via an attribute ​

Declare the injection of container parameters via the \Kaspi\DiContainer\Attributes\Parameter attribute into the constructor of the PHP class \App\Services\Foo.

php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Enums\SetupEnum;
use Kaspi\DiContainer\Attributes\Parameter;

final class Foo
{
    public function __construct(
        public readonly Bar $bar,
        // Equivalent to the declaration #[Parameter('setupType')]
        #[Parameter]
        public readonly SetupEnum $setupType,
        #[Parameter('emails.notify')]
        public readonly string $mailTo,
        #[Parameter('app.debug')]
        public readonly bool $debug,
    ) {}
}
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;

use function {getenv, putenv};

putenv('APP_ENV=dev');

$builder = (new DiContainerBuilder())
    ->loadParameters('/app/config/params.php')
    ->loadParameters('/app/config/params_notify.php');

if ('dev' === getenv('APP_ENV')) {
    $builder->loadParameters('/app/config/params_dev.php');
}

$container = $builder->build();

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

var_dump($foo->bar);
var_dump($foo->setupType);
var_dump($foo->mailTo);
var_dump($foo->debug);

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\Bar)#2 (0) {
}
enum(App\Enums\SetupEnum::LOCAL)
string(33) "admin@localhost, manager@localhost"
bool(true)

  1. The callable type represents values ​​or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎