Skip to content

Helper function diValue() ​

The diValue() helper function injects a dependency "as is."

Function signature:

php
use Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionTagArgumentInterface;
 
\Kaspi\DiContainer\diValue(
    mixed $value
): DiDefinitionTagArgumentInterface

Parameters:

  • $value – the value passed into the container.

The diValue() function returns an object that provides the following methods:

  • bindTag() – adds a tag containing metadata for identification.

bindTag() ​

Tags allow a declared definition to be associated with a collection of elements marked with the same tag.

The bindTag() method is defined by the \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionTagArgumentInterface interface.

Method signature:

php
DiDefinitionTagArgumentInterface::bindTag(
    string $name,
    array $options = [],
    null|int|string $priority = null
)

TIP

For more details, see the guide on working with tags.

Container identifier ​

To make a declared definition accessible via the container's get() and has() methods, you must specify a container identifier[1] in the configuration file that corresponds to the declared definition.

php
<?php
declare(strict_types=1);

// file: /app/src/container.php

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

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

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

$emails = $container->get('tasks.emails');

var_dump($email);
php
<?php
declare(strict_types=1);

// file: /app/config/emails.php

use function Kaspi\DiContainer\diValue;

return static function(): Generator {
     yield 'tasks.emails' => diValue([
        'runner@company.inc',
        'monitor@company.inc',
    ]);
};

Run the following command in the terminal:

shell
php /app/src/container.php

A message like the following will appear:

console
array(2) {
  [0]=>
  string(18) "runner@company.inc"
  [1]=>
  string(19) "monitor@company.inc"
}

  1. A container ID is a string name (or a class/interface name) used to register a service or object within a container and to retrieve it via the get() method. ↩︎