Helper function diValue()
The diValue() helper function injects a dependency "as is."
Function signature:
use Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionTagArgumentInterface;
\Kaspi\DiContainer\diValue(
mixed $value
): DiDefinitionTagArgumentInterfaceParameters:
$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:
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
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
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:
php /app/src/container.phpA message like the following will appear:
array(2) {
[0]=>
string(18) "runner@company.inc"
[1]=>
string(19) "monitor@company.inc"
}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. ↩︎