Skip to content

Example of implementation for a specific parameter ​

When declaring an injection into a method parameter or a callable type[1] that expects an object implementing a specific interface, you can specify exactly which implementation should be injected. In such a declaration, the PHP class being injected may differ from the globally declared implementation of the interface.

PHP classes ​

php
<?php
declare(strict_types=1);

// file: /app/src/Interfaces/QuxInterface.php

namespace App\Interfaces;

interface QuxInterface
{
    // …
}
php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Interfaces\QuxInterface;

final class Foo
{
    public function __construct(
        public readonly QuxInterface $qux
    ) {}
}
php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Interfaces\QuxInterface;

final class Qux
{
    public function __construct(
        public readonly QuxInterface $qux
    ) {}
}
php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Interfaces\QuxInterface;

final class Bar implements QuxInterface
{
    // …
}
php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Interfaces\QuxInterface;

final class Baz implements QuxInterface
{
    // …
}

You need to register the \App\Services\Bar PHP class as an implementation of the \App\Interfaces\QuxInterface interface, so that the container can automatically inject it into method parameters based on the configuration.

Additionally, for the \App\Services\Qux PHP class, you need to configure dependency injection into the $qux constructor parameter of the \App\Services\Baz PHP class; note that the class being injected implements the \App\Interfaces\QuxInterface interface.

Helper functions ​

In the services.php configuration file, the primary implementation of the \App\Interfaces\QuxInterface interface is specified as the PHP class \App\Services\Bar.

php
<?php
declare(strict_types=1);

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

use App\Interfaces\QuxInterface;
use App\Service\Bar;

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    yield QuxInterface::class => diAutowire(Bar::class);
};

This means that an object of the PHP class \App\Service\Bar will be injected into any parameter with the type \App\Interfaces\QuxInterface.

diAutowire() ​

The configuration file services_qux.php uses the helper function diAutowire() to declare injection into the $qux constructor parameter of the PHP class \App\Services\Qux.

NOTE

Here is a simple example of injection via a helper function. When defining the injection of a PHP class using a helper function, all the features of that helper function are available — including constructor argument binding and injection via setter methods.

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, Qux};

$container = (new DiContainerBuilder())
    ->import(namespace: 'App\\', src: '/app/src')
    ->load('/app/config/services.php')
    ->load('/app/config/services_qux.php')
    ->build();

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

$qux = $container->get(Qux::class);
var_dump($qux->qux);
php
<?php
declare(strict_types=1);

// file: /app/config/services_qux.php

use App\Services\{Qux, Baz};

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    yield diAutowire(Qux::class)
        ->bindArguments(
            qux: diAutowire(Baz::class)
        );
};
php
<?php
declare(strict_types=1);

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

use App\Interfaces\QuxInterface;
use App\Service\Bar;

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    yield QuxInterface::class => diAutowire(Bar::class);
};

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) {
}
object(App\Services\Baz)#4 (0) {
}

diCallable() ​

In some cases, when complex initialization of a PHP class is required, the process of creating such an object can be delegated to any callable type[1:1].

The configuration file services_qux.php uses the helper function diCallable() to declare a PHP class via an anonymous function.

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, Qux};

$container = (new DiContainerBuilder())
    ->import(namespace: 'App\\', src: '/app/src')
    ->load('/app/config/services.php')
    ->load('/app/config/services_qux.php')
    ->build();

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

$qux = $container->get(Qux::class);
var_dump($qux->qux);
php
<?php
declare(strict_types=1);

// file: /app/config/services_qux.php

use App\Services\{Qux, Baz};

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

return static function (): Generator {
    // Сложная сборка PHP-класса
    $fn = static function (): Baz {
        $baz = new Baz();
        // Additional operations with $baz are possible here.
        return $baz;
    };

    yield diAutowire(Qux::class)
        ->bindArguments(
            qux: diCallable($fn)
        );
};
php
<?php
declare(strict_types=1);

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

use App\Interfaces\QuxInterface;
use App\Service\Bar;

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    yield QuxInterface::class => diAutowire(Bar::class);
};

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) {
}
object(App\Services\Baz)#4 (0) {
}

TIP

A similar declaration can be made using the helper function diFactory().


diGet() ​

If a PHP class definition — complete with all necessary dependency injections and configurations — already exists in the container and is accessible via its get() method, then when defining an implementation for an interface, you can simply provide a reference to the identifier for that PHP class as it exists within the container.

The configuration file services_qux.php uses the helper function diGet() to declare an injection into a 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, Qux};

$container = (new DiContainerBuilder())
    ->import(namespace: 'App\\', src: '/app/src')
    ->load('/app/config/services.php')
    ->load('/app/config/services_qux.php')
    ->build();

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

$qux = $container->get(Qux::class);
var_dump($qux->qux);
php
<?php
declare(strict_types=1);

// file: /app/config/services_qux.php

use App\Services\{Qux, Baz};

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

return static function (): Generator {
    yield diAutowire(Baz::class);

    // … many lines

    yield diAutowire(Qux::class)
        ->bindArguments(
            qux: diGet(Baz::class)
        );
};
php
<?php
declare(strict_types=1);

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

use App\Interfaces\QuxInterface;
use App\Service\Bar;

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    yield QuxInterface::class => diAutowire(Bar::class);
};

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) {
}
object(App\Services\Baz)#4 (0) {
}

Attributes ​

The implementation of the \App\Interfaces\QuxInterface interface is declared by the PHP class \App\Services\Bar globally via the \Kaspi\DiContainer\Attributes\Service attribute.

php
<?php
declare(strict_types=1);

// file: /app/src/Interfaces/QuxInterface.php

namespace App\Interfaces;

use App\Service\Bar;
use Kaspi\DiContainer\Attributes\Service;

#[Service(Bar::class)]
interface QuxInterface
{
    // …
}

This means that an object of the PHP class \App\Service\Bar will be injected into any parameter with the type \App\Interfaces\QuxInterface.


Autowire ​

The $qux constructor parameter of the PHP class \App\Services\Qux is typed as \App\Interfaces\QuxInterface. You need to specify that a different implementation of this interface — specifically, the PHP class \App\Services\Baz — should be injected into this parameter. This can be done using the \Kaspi\DiContainer\Attributes\Autowire attribute.

NOTE

Here is a simple example of using an attribute. When declaring the injection of a PHP class via an attribute, all of the attribute's features are available, including constructor argument declarations and injections via setter methods.

php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Interfaces\QuxInterface;
use Kaspi\DiContainer\Attributes\Autowire;

final class Qux
{
    public function __construct(
        #[Autowire(Bar::class)]
        public readonly QuxInterface $qux
    ) {}
}
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, Qux};

$container = (new DiContainerBuilder())
    ->import(namespace: 'App\\', src: '/app/src')
    ->build();

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

$qux = $container->get(Qux::class);
var_dump($qux->qux);

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) {
}
object(App\Services\Baz)#4 (0) {
}

InjectByCallable ​

Declaring an interface implementation can require complex PHP class assembly, so this process can be delegated to any callable type[1:2].

In this example, the called type is the static method of the PHP class - \App\Helpers\FactoryCreate::makeService(), and the injection can be declared using the attribute \Kaspi\DiContainer\Attributes\InjectByCallable.

php
<?php
declare(strict_types=1);

// file: /app/src/Helpers/FactoryCreate.php

namespace App\Helpers;

use App\Services\Baz;

final class FactoryCreate
{
    // Complex PHP class construction
    public static function makeService(): Baz
    {
        $baz = new Baz();
        // Additional operations with $baz are possible here.
        return $baz;
    }
}
php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Interfaces\QuxInterface;
use Kaspi\DiContainer\Attributes\InjectByCallable;
use App\Helpers\FactoryCreate;

final class Qux
{
    public function __construct(
        #[InjectByCallable([
            FactoryCreate::class,
            'makeService',
        )]
        public readonly QuxInterface $qux
    ) {}
}
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, Qux};

$container = (new DiContainerBuilder())
    ->import(namespace: 'App\\', src: '/app/src')
    ->build();

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

$qux = $container->get(Qux::class);
var_dump($qux->qux);

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) {
}
object(App\Services\Baz)#4 (0) {
}

Inject ​

If a PHP class has already been registered in the container — with all necessary injections and configurations specified — and is accessible via the container's get() method, you can use the \Kaspi\DiContainer\Attributes\Inject attribute to declare an injection into a parameter.

php
<?php
declare(strict_types=1);

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

namespace App\Services;

use App\Interfaces\QuxInterface;
use Kaspi\DiContainer\Attributes\Inject;

final class Qux
{
    public function __construct(
        #[Inject(Bar::class)]
        public readonly QuxInterface $qux
    ) {}
}
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, Qux};

$container = (new DiContainerBuilder())
    ->import(namespace: 'App\\', src: '/app/src')
    ->build();

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

$qux = $container->get(Qux::class);
var_dump($qux->qux);

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) {
}
object(App\Services\Baz)#4 (0) {
}

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