lukman-ss / container
A lightweight dependency injection container for PHP.
v1.0.0
2026-06-13 09:24 UTC
Requires
- php: >=8.2
Requires (Dev)
- phpunit/phpunit: ^10.5
README
A lightweight dependency injection container for PHP 8.2+ with zero runtime dependencies.
Features
- Bindings, singletons, and existing instances
- Alias, remove, and flush helpers
- Reflection constructor auto-wiring
- Contextual binding for class dependencies
- Tags for grouped lazy resolution
- Method injection via
call() - Service providers
Requirements
- PHP 8.2 or higher
Installation
composer require lukman-ss/container
Basic Usage
use Lukman\Container\Container; $container = new Container(); $container->bind('config', ['debug' => true]); $config = $container->get('config');
Bindings
use Lukman\Container\Container; final class Logger { } $container = new Container(); $container->bind(Logger::class); $loggerA = $container->get(Logger::class); $loggerB = $container->get(Logger::class);
Singletons
$container->singleton(Logger::class); $loggerA = $container->get(Logger::class); $loggerB = $container->get(Logger::class);
Instances
$logger = new Logger(); $container->instance(Logger::class, $logger); $sameLogger = $container->get(Logger::class);
Auto-wiring
final class UserRepository { public function __construct(public Logger $logger) { } } $repository = $container->get(UserRepository::class);
Aliases
$container->bind(Logger::class); $container->alias(Logger::class, 'logger'); $logger = $container->get('logger');
Contextual Binding
interface LoggerInterface { } final class FileLogger implements LoggerInterface { } final class Worker { public function __construct(public LoggerInterface $logger) { } } $container->when(Worker::class) ->needs(LoggerInterface::class) ->give(FileLogger::class); $worker = $container->get(Worker::class);
Tags
$container->bind('first_logger', fn () => new Logger()); $container->bind('second_logger', fn () => new Logger()); $container->tag(['first_logger', 'second_logger'], 'loggers'); $loggers = $container->tagged('loggers');
Method Injection
$result = $container->call(function (Logger $logger): Logger { return $logger; });
Service Providers
use Lukman\Container\Container; use Lukman\Container\ServiceProviderInterface; final class AppServiceProvider implements ServiceProviderInterface { public function register(Container $container): void { $container->singleton(Logger::class); } public function boot(Container $container): void { } } $container->register(AppServiceProvider::class); $container->boot();
Tests
composer test
