Small Dependency Injection



This content originally appeared on DEV Community and was authored by sebk69

Introducing small/dependency-injection

I’ve just released small/dependency-injection — a lightweight, framework-agnostic Dependency Injection container for PHP 8.4.

Quick Start

Install via Composer:

composer require small/dependency-injection

Define your container configuration:

<?php

use Small\DependencyInjection\AbstractContainerConfiguration;
use function Small\DependencyInjection\injectService;
use function Small\DependencyInjection\injectParameter;

class AppConfig extends AbstractContainerConfiguration
{
    public function configure(): self
    {
        $this->parameter('secret', 'top-secret')
             ->service(
                 MyServiceInterface::class,
                 MyService::class,
                 [
                     injectParameter('secret'),
                     injectService(LoggerInterface::class),
                 ]
             )
             ->autowireNamespace('App');

        return $this;
    }
}

Use it:

$config = new AppConfig(__DIR__ . '/composer.json');
$config->loadParameters($container);

$myService = $container->get(MyServiceInterface::class);

Links

That’s it — a simple, modern DI container for your next PHP project.


This content originally appeared on DEV Community and was authored by sebk69