Inject attribute
The \Kaspi\DiContainer\Attributes\Inject attribute injects a dependency via a container identifier [1]; this is equivalent to calling the container's get() method.
In cases where a parameter of a method or a callable type[2] is defined using custom types (such as PHP classes, built-in PHP classes, or enumerations), the Inject attribute is not required — the container will automatically inject the dependency based on the configuration.
The Inject attribute is necessary in cases where the container cannot automatically determine the dependency type for a specific method parameter or a type being instantiated; in such situations, the type of the object to be injected must be specified explicitly. For example, if a method parameter is defined as mixed or as a composite type (an intersection or union types), this attribute allows you to precisely specify which value from the container should be injected into that parameter.
Attribute configuration:
\Attribute::TARGET_PARAMETER\Attribute::IS_REPEATABLE
Attribute signature:
\Kaspi\DiContainer\Attributes\Inject::__construct(
string $id = ''
)Parameters:
$id- container identifier [1:1].
Example of injection into a parameter of type mixed
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
final class Foo
{
public function __construct(
public readonly mixed $entry
) {}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
final class Bar
{
// …
}It is necessary to inject an object of the \App\Services\Bar class into the $entry constructor parameter of the \App\Services\Foo PHP class.
Since the $entry constructor parameter of the \App\Services\Foo class is typed as mixed, the container cannot automatically determine which type of dependency should be injected. We will specify the required object for injection using the \Kaspi\DiContainer\Attributes\Inject attribute.
<?php
declare(strict_types=1);
// file: /app/src/Services/Foo.php
namespace App\Services;
use Kaspi\DiContainer\Attributes\Inject;
final class Foo
{
public function __construct(
#[Inject(Bar::class)]
public readonly mixed $entry
) {}
}<?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, Bar};
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->entry instanceof Bar);Run the following command in the terminal:
php /app/src/container_build.phpA message like the following will appear:
(bool) trueCookbook
The "Recipes" section provides examples of how to refine an ad by incorporating parameters:
- Dependency injection of a PHP class that implements a specific interface.
- Dependency injection for union type parameters.
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. ↩︎ ↩︎The
callabletype represents values or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎