Skip to content

Service attribute ​

The \Kaspi\DiContainer\Attributes\Service attribute tells the PHP interface its implementation.

Attribute configuration:

  • \Attribute::TARGET_CLASS

Attribute signature:

php
\Kaspi\DiContainer\Attributes\Service::__construct(
    string $id,
    ?bool $isSingleton = null
)

Parameters:

  • $id – container identifier [1].
  • $isSingleton – The container will return the same object instance (singleton pattern). If set to null, the value will be determined based on the container configuration.

The $id parameter specifies the implementation of an interface via a PHP class, which is accessible within the container through the get() method.

Declaring an interface implementation using the \Kaspi\DiContainer\Attributes\Service attribute is global in scope; if no specific dependency injection instructions are provided for a given parameter, the container will resolve the dependency based on the interface configuration by retrieving the object from the container via the get() method, using the identifier specified in the $id parameter.

For example, specifying the string identifier 'services.qux' for the $id parameter means that the container should inject the object obtained by calling the $container->get('services.qux') method.

Example of interface configuration ​

PHP classes and interfaces:

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/Interfaces/QuxInterface.php

namespace App\Interfaces;

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

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

namespace App\Services;

use App\Interfaces\QuxInterface;

final class Bar implements QuxInterface
{
    // …
}

It is necessary to declare globally that the PHP class \App\Services\Bar implements the interface \App\Interfaces\QuxInterface.

This means that, based on the configuration, the container will inject the PHP class \App\Services\Bar into any method or function parameters that are type-hinted with \App\Interfaces\QuxInterface.

php
<?php
declare(strict_types=1);

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

namespace App\Interfaces;

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

#[Service(Bar::class)]
interface QuxInterface
{
    // …
}
php
<?php
declare(strict_types=1);

// file: /app/src/container.php

require_once __DIR__.'/../vendor/autoload.php';

use Kaspi\DiContainer\DiContainerBuilder;
use App\Services\{Foo, Bar};
use App\Interfaces\QuxInterface;

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

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

var_dump($foo->qux instanceof QuxInterface);
var_dump($foo->qux instanceof Bar);

Run the following command in the terminal:

shell
php /app/src/container.php

A message like the following will appear:

console
bool(true)
bool(true)

Configuration of third-party interfaces ​

When configuring dependency injection for a parameter typed as an interface—such as one provided by a third-party PHP package — you must specify the implementation of that interface via configuration files or a collection during the container-building process.

php
<?php
declare(strict_types=1);

use Psr\Log\LoggerInterface;
use App\Services\MyLogger;

use function Kaspi\DiContainer\diAutowire;

return static function (): Generator {
    LoggerInterface::class => diAutowire(MyLogger::class);
};

An example showing how to implement a PHP class that satisfies a specific third-party interface can be found in the Cookbook section.


  1. 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. ↩︎