Helper function diRuntime()
The helper function diRuntime() declares a PHP class within the container; its actual state is initialized outside the container, and its value is set only at runtime.
In the context of a dependency injection container, such a PHP class is called a “Runtime Definition”.
WARNING
A helper function cannot be applied to the parameters of a method or function. To inject dependencies into the parameters of methods or callable types, use the helper function diGet().
Function signature:
use Kaspi\DiContainer\Interfaces\DiDefinition\{
DiDefinitionTagArgumentInterface as Tag,
DiDefinitionResetterSetterInterface as Resetter
};
\Kaspi\DiContainer\diRuntime(
string $containerIdentifierOrClass,
?string $message = null,
?string $classDefinition = null
): Tag & ResetterParameters:
$containerIdentifierOrClass– the container identifier[1] for a "runtime definition".$message– the error message.$classDefinition– a fallback FQCN[2] declaration for the object.
The diRuntime() function returns an object that provides the following methods:
bindTag()– add a tag with metadata.setResetter()– set a value used to reset the object's state.
TIP
Read the documentation section "Configuring an object in the runtime container".
bindTag()
Tags allow a "runtime 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.
setResetter()
A value used for resetting an object's state.
The setResetter() method is defined by the \Kaspi\DiContainer\Interfaces\DiDefinition\DiDefinitionResetterSetterInterface interface.
DiDefinitionResetterSetterInterface::setResetter(
callable|false|string $resetter
)TIP
See the detailed information in the section “Resetting object state in long-running processes”.
Container identifier for Runtime Definition
The diRuntime() helper function can pass an identifier [1:1] to the container via the $containerIdentifierOrClass parameter, making it accessible through the container's get() and has() methods.
<?php
declare(strict_types=1);
// file: /app/config/runtime_definitions.php
use App\Core\Kernel;
use function Kaspi\DiContainer\diRuntime;
return static function (): Generator {
yield Kernel::class => diRuntime(Kernel::class);
yield diRuntime(Kernel::class);
};NOTE
The configuration file example above allows you to retrieve an object using the container method $container->get(App\Core\Kernel::class).
It is also possible to declare an object with an arbitrary string-based container identifier[1:2]:
<?php
declare(strict_types=1);
// file: /app/config/runtime_definitions.php
use App\Core\Kernel;
use function Kaspi\DiContainer\diRuntime;
return static function (): Generator {
yield 'services.core_kernel' => diRuntime(Kernel::class);
};NOTE
The configuration file example above allows you to retrieve an object using the container method $container->get('services.core_kernel').