A PSR-11 compliant dependency injection and service container library for modern PHP applications.
Artex DI-Container is a lightweight, feature-rich library designed to simplify dependency management in PHP. Built with high performance, flexibility, and extensibility in mind, this library combines the power of dependency injection with a robust service container.
- PSR-11 Compliant: Adheres to the
ContainerInterface
standard for maximum interoperability. - Dependency Injection: Autowire classes and resolve dependencies effortlessly.
- Service Management: Tagged services, lifecycle hooks, deferred loading, and more.
- Flexibility: Perfect for frameworks, microservices, or standalone applications.
composer "require artex/di-container"
use Artex\DIContainer\ServiceContainer;
$container = ServiceContainer::getInstance();
// Register a shared service
$container->singleton('logger', function () {
return new Logger();
});
// Resolve the service
$logger = $container->get('logger');
$container->set('handler1', new Handler1(), true, ['event']);
$container->set('handler2', new Handler2(), true, ['event']);
// Retrieve all services tagged with "event"
$handlers = $container->getByTag('event');
$container->onRegister(function ($id, $concrete, $shared) {
echo "Service {$id} registered.\n";
});
$container->onResolve(function ($id, $instance) {
echo "Service {$id} resolved.\n";
});
$container->singleton('db', function () {
return new DatabaseConnection();
});
Run tests using PHPUnit:
./vendor/bin/phpunit
- James Gober - Developer
- Artex Software - Sponsor
The Apache License 2.0. Please see LICENSE file for more information.