Introduction
Dependency injection is an important design pattern in software development. The core idea is that classes do not create the objects they need; instead, they receive them from an external source. This ensures code independence and makes the code more reusable and easier to test.
Dependency injection is a technique for writing better-quality code.
A dependency injection container is a tool that helps automatically assemble objects and inject their dependencies based on configuration rules or autowiring.
Let’s look at a simplified example using a set of PHP classes. These classes illustrate the classic pattern of passing dependencies via a constructor:
<?php
declare(strict_types=1);
// file: /app/src/Controllers/PostController.php
namespace App\Controllers;
use App\Services\Mail;
use App\Models\Post;
// Controller for processing an action
final class PostController
{
public function __construct(private Mail $mail) {}
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/Mail.php
namespace App\Services;
// Email sending service
final class Mail
{
public function __construct(private Envelope $envelope) {}
// …
}<?php
declare(strict_types=1);
// file: /app/src/Services/Envelope.php
namespace App\Services;
// Class for creating a message
final class Envelope
{
// ...
}Now, let’s imagine that to obtain an instance of the \App\Controllers\PostController class, we need to pass an instance of the App\Services\Mail class into its constructor – a class which, in turn, depends on an instance of the \App\Services\Envelope class.
This results in a chain of object creation and dependency passing via the constructor:
<?php
declare(strict_types=1);
// file: /app/src/index.php
require_once __DIR__ . '/../vendor/autoload.php';
$envelop = new \App\Services\Envelope();
$mail = new \App\Services\Mail($envelop);
$controller = new \App\Controllers\PostController($mail);A dependency injection container is designed to make dependency injection more practical and to automate dependency injection operations based on a provided configuration.
DiContainer overview
Kaspi/di-container is a dependency injection container for PHP >= 8.1 that implements the PSR-11.
Features
- Autowiring: automatic dependency injection — you declare dependencies using type hints[1], and the container handles the rest. In most cases, you do not need to define object configuration;
- Lazy injection: allows for the deferred creation of object dependencies until the moment they are actually used;
- Attribute-driven: declarative dependency injection using native PHP 8+ attributes;
- Zero configuration: no object configuration is required if a PHP class has no dependencies or depends only on other concrete classes;
- Definition Tags: allows for the injection or retrieval of a collection of items from the container that share the same tag name;
- Compiling the Container: generates a configured container as high-performance PHP code, optimized specifically for your configuration and classes;
- PSR-11: compliant with the PSR-11 standard for dependency injection containers;
- Object state reset: a mechanism for resetting the state of objects within long-running processes;
Install
Use the PHP dependency manager composer to install the "kaspi/di-container" package.
composer require kaspi/di-containerQuck start
Let’s look at a simplified example involving a set of PHP classes for which we need to implement automatic dependency injection using the "kaspi/di-container" dependency injection container.
PHP classes:
<?php
declare(strict_types=1);
// file: /app/src/Controllers/PostController.php
namespace App\Controllers;
use App\Services\Mail;
use App\Models\Post;
// Controller for processing an action
final class PostController
{
public function __construct(private Mail $mail) {}
public function send(Post $post): bool
{
$this->mail->envelop()
->subject('Publication success')
->message('Post <'.$post->title.'> was published.');
return $this->mail->send();
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Mail.php
namespace App\Services;
// Email sending service
final class Mail
{
public function __construct(private Envelope $envelope) {}
public function envelop(): Envelope
{
return $this->envelope;
}
public function send(): bool
{
// sending message
return true;
}
}<?php
declare(strict_types=1);
// file: /app/src/Services/Envelope.php
namespace App\Services;
// Class for creating a message
final class Envelope
{
public function subject(string $subject): void
{
// ...
}
public function message(string $message): void
{
// ...
}
}<?php
declare(strict_types=1);
// file: /app/src/Models/Post.php
namespace App\Models;
// Data model — blog post
class Post
{
public string $title;
// ...
}In action:
<?php
declare(strict_types=1);
use Kaspi\DiContainer\DiContainerBuilder;
use App\Controllers\PostController;
use App\Models\Post;
$container = (new DiContainerBuilder())
->build();
if ($container->has(PostController::class)) {
// An object with dependencies injected via the constructor
$postController = $container->get(PostController::class);
// let's fill in the post details
$post = new Post();
$post->title = 'Lorem ipsum';
// calling a method on the obtained object
$postController->send($post);
}<?php
declare(strict_types=1);
use Kaspi\DiContainer\DiContainerBuilder;
use App\Controllers\PostController;
use App\Models\Post;
$container = (new DiContainerBuilder())
->build();
// let's fill in the post details
$post = new Post();
$post->title = 'Lorem ipsum';
$container->call([PostController::class, 'send'], $post);PSR-11
The container is fully compatible with the PSR-11 recommendations.
<?php
declare(strict_types=1);
namespace Psr\Container;
interface ContainerInterface
{
public function get($id);
public function has($id);
}call()
The container implements the \Kaspi\DiContainer\Interfaces\DiContainerCallInterface interface, which provides the call() method for callable types or values that can be converted by the container to the callable type.
<?php
declare(strict_types=1);
use App\Services\Foo;
use Kaspi\DiContainer\DiContainerBuilder;
use function var_export;
$container = (new DiContainerBuilder())->build();
$container->call(
static function(Foo $foo, string $name): void {
$foo->doSomethind($name);
sprintf('Call Foo::doSomething(%s)', var_export($name, true));
},
name: 'John'
);
// string(29) "Call Foo::doSomething('John')"More details about the call() method are provided in the section “Container method for callable types”.
set()
The container implements the \Kaspi\DiContainer\Interfaces\DiContainerSetterInterface interface, which provides the set() method for directly adding an object (an instantiated class) to an already assembled dependency container.
<?php
declare(strict_types=1);
use App\Services\Bar;
$bar = new Bar(
// set some dependencies.
);
// the identifier will be specified as 'App\Services\Bar'
$container->set($bar::class, $bar);More details about the set() method are provided in the section “Dynamically Adding Definitions to a Container”.
Implementation Features of Certain Classes and Interfaces
When injecting dependencies of the types listed below, the current container will always be obtained:
\Psr\Container\ContainerInterface\Kaspi\DiContainer\Interfaces\DiContainerInterface\Kaspi\DiContainer\DiContainer
Example:
<?php
declare(strict_types=1);
use Kaspi\DiContainer\DiContainerBuilder;
use Psr\Container\ContainerInterface;
class Foo
{
public function __construct(
public readonly ContainerInterface $container
) {}
}
$container = (new DiContainerBuilder())->build();
var_dump($container === $container->get(Foo::class)->container);
// (bool) trueDependency injection declaration
Dependency injection can be defined in configuration files or via attributes; it is also possible to combine both methods of definition.
To build the container, use the special DiContainerBuilder class.
Contents of the documentation
- 👷♂️ Building the dependency container.
- ⚙️ Configuration for DiContainer.
- 🐘 Declaration in configuration files.
- #️⃣ Declaration via attributes.
- 📦 The
call()container method for callable types. - 🔖 Tags – labels for container definitions.
- 📋 Container parameters.
- 🗳️ Declaring an object in a runtime container.
- 🏭 Using factory methods for dependency injection.
- 🧹 Dependency container reset.
- ♻️ Resetting object state for long-running processes.
- 💤 Injection of lazy objects by the container.
Type hints – the specification of an expected data type for method parameters, such as the name of a specific class or interface. ↩︎