Example of a global interface implementation declaration
All typed parameters of a method or a callable type[1] must implement the same PHP class, which is declared as an implementation of a specific interface.
PHP classes
<?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
declare(strict_types=1);
// file: /app/src/Interfaces/QuxInterface.php
namespace App\Interfaces;
interface QuxInterface
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
use App\Interfaces\QuxInterface;
final class Bar implements QuxInterface
{
// …
}<?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 the implementation of the \App\Interfaces\QuxInterface interface. The container should automatically inject this registered implementation into method parameters, without requiring any additional declarations.
Note that while the \App\Services\Baz PHP class also implements \App\Interfaces\QuxInterface, it will not be injected into any parameter without an explicit declaration.
Helper functions
For this task, you need to declare the PHP class \App\Services\Bar in the configuration file and specify the interface's FQCN[2] as the container identifier[3].
Below, we will look at ways to declare an interface implementation using various helper functions.
diAutowire()
The services.php configuration file uses the diAutowire() helper function to declare a PHP class, with the \App\Interfaces\QuxInterface interface specified as the container identifier.
NOTE
This is a simple example of a declaration; however, when declaring a PHP class, all methods of this helper function are available—including those for defining constructor injection and injection via setter methods.
<?php
declare(strict_types=1);
// file: /app/src/container.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->load('/app/config/services.php')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->qux);<?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:
php /app/src/container.phpA message like the following will appear:
object(App\Services\Bar)#2 (0) {
}diCallable()
When declaring an interface implementation, the construction of a PHP class may be complex; therefore, this process can be delegated to any callable type[1:1].
The services.php configuration file uses the diCallable() helper function to define a PHP class via an anonymous function, with the \App\Interfaces\QuxInterface interface serving as the container identifier.
Additionally, in this interface implementation declaration, the $isSingleton parameter of the helper function is set to true, ensuring that the container retrieves the same object instance rather than creating a new one each time.
<?php
declare(strict_types=1);
// file: /app/src/container.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->load('/app/config/services.php')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->qux);<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Interfaces\QuxInterface;
use App\Service\Bar;
use function Kaspi\DiContainer\diCallable;
return static function (): Generator {
// Сложная сборка PHP-класса
$fn = static function (): Bar {
$bar = new Bar();
// тут возможны дополнительные действия с $bar
return $bar;
};
yield QuxInterface::class => diCallable($fn, isSingleton: true);
};Run the following command in the terminal:
php /app/src/container.phpA message like the following will appear:
object(App\Services\Bar)#2 (0) {
}TIP
A similar declaration can be made using the helper function diFactory().
diGet()
If a PHP class definition—complete with all necessary injections and configurations—already exists within the container, you can simply reference the existing container identifier for that PHP class when declaring an implementation of an interface.
The services.php configuration file uses the helper function diGet() to register a PHP class as an implementation of the \App\Interfaces\QuxInterface interface.
<?php
declare(strict_types=1);
// file: /app/src/container.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->load('/app/config/services.php')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->qux);<?php
declare(strict_types=1);
// file: /app/config/services.php
use App\Interfaces\QuxInterface;
use App\Service\Bar;
use function Kaspi\DiContainer\{diAutowire, diGet};
return static function (): Generator {
yield diAutowire(Bar::class);
// … many lines
yield QuxInterface::class => diGet(Bar::class);
};Run the following command in the terminal:
php /app/src/container.phpA message like the following will appear:
object(App\Services\Bar)#2 (0) {
}Attributes
The \Kaspi\DiContainer\Attributes\Service attribute allows you to globally register a specific PHP class as the implementation of an interface within the container. This means the container will automatically inject this registered implementation whenever a dependency on that interface is required — unless a different implementation has been explicitly specified for a particular method parameter or type being invoked.
Service and FQCN
You need to add an attribute to the \App\Interfaces\QuxInterface interface and specify its implementation via the PHP class \App\Services\Bar.
The $id parameter of the attribute’s constructor accepts a string value that tells the container which identifier to use to retrieve an object implementing this interface; in this case, you can provide the value 'App\Services\Bar' or use the constant \App\Services\Bar::class.
<?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
{
// …
}<?php
declare(strict_types=1);
// file: /app/src/container.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->qux);Run the following command in the terminal:
php /app/src/container.phpA message like the following will appear:
object(App\Services\Bar)#2 (0) {
}Service and its significance for the $id parameter
The documentation for the \Kaspi\DiContainer\Attributes\Service attribute states that the $id constructor parameter must serve as a container identifier. This means that if an object implementing the declared interface can be retrieved via the container's get() method, then that value is valid for the $id parameter.
Consider an example where an implementation of an interface is retrieved using the container identifier 'services.bar', which is declared for the PHP class \App\Services\Bar via the \Kaspi\DiContainer\Attributes\Autowire attribute.
Declaring an interface implementation in this way can be useful when the same PHP class needs to be registered in the container multiple times – each instance having different injected dependencies and a unique container identifier.
<?php
declare(strict_types=1);
// file: /app/src/Interfaces/QuxInterface.php
namespace App\Interfaces;
use App\Service\Bar;
use Kaspi\DiContainer\Attributes\Service;
#[Service('services.bar')]
interface QuxInterface
{
// …<?php
declare(strict_types=1);
// file: /app/src/Services/Bar.php
namespace App\Services;
use App\Interfaces\QuxInterface;
use Kaspi\DiContainer\Attributes\Autowire;
#[Autowire(id: 'services.bar')]
final class Bar implements QuxInterface
{
// …<?php
declare(strict_types=1);
// file: /app/src/container.php
require_once __DIR__.'/../vendor/autoload.php';
use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\Foo;
$container = (new DiContainerBuilder())
->import(namespace: 'App\\', src: '/app/src')
->build();
$foo = $container->get(Foo::class);
var_dump($foo->qux);Run the following command in the terminal:
php /app/src/container.phpA message like the following will appear:
object(App\Services\Bar)#2 (0) {
}The
callabletype represents values or expressions that can be invoked as a function. See the description in the PHP documentation. ↩︎ ↩︎Fully Qualified Class Name – the full name of a class or interface, including its namespace. ↩︎
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. ↩︎