A lightweight dependency injection container implementing PSR-11.
composer require georgeff/containerRegister a definition by providing an ID and a callable factory. The container instance is passed to the factory.
use Georgeff\Container\Container;
$container = new Container();
$container->add('database', function (Container $container) {
return new DatabaseConnection('localhost', 'mydb');
});Shared definitions are resolved once and the same instance is returned on subsequent calls.
$container->addShared('database', function (Container $container) {
return new DatabaseConnection('localhost', 'mydb');
});
// Or pass true as the third argument to add()
$container->add('database', function (Container $container) {
return new DatabaseConnection('localhost', 'mydb');
}, true);$db = $container->get('database');Aliases allow you to resolve a definition by an alternate name, useful for binding interfaces to implementations.
$container->addShared(DatabaseConnection::class, function (Container $container) {
return new DatabaseConnection('localhost', 'mydb');
});
$container->addAlias(DatabaseConnection::class, ConnectionInterface::class);
// Resolves the DatabaseConnection definition
$db = $container->get(ConnectionInterface::class);$container->has('database'); // true
$container->has('nonexistent'); // falseDefinitionNotFoundException— thrown when getting a definition that does not exist or aliasing a non-existing definition. Implements PSR-11NotFoundExceptionInterface.CircularDependencyException— thrown when a circular dependency is detected during resolution. Implements PSR-11ContainerExceptionInterface.ContainerException— thrown when an error occurs during definition resolution. Implements PSR-11ContainerExceptionInterface.
MIT