Dependency injection PHP FIG PSR-11 container implementation
php > 7.1
via Composer
$ composer require flotzilla/container
Init service container with constructor parameters
$container = new \flotzilla\Container\Container(
[
'EmptyTestClassDI' => EmptyTestClass::class, // class without dependencies, init by classname
'TestClassDI' => [TestClass::class, 'message'], // class with constructor string parameter
'TestClassDI2' => function () { // closure, that returns new class instance
return new TestClass('test');
},
'ClosureDI' => function ($x, $y) { // closure, that returns sum result
return $x + $y;
},
'TestClassWithDependecyDI' => [TestClassWithDependecy::class, 'TestClassDI'] // class with dependency of another service
]
);
Or with setter
use \flotzilla\Container\Container;
$container = new Container();
$container->set('LoggerDI', function () { return new Logger();});
$container->set('ClosureDI', function ($x, $y) { return $x + $y;});
$container->set('EmptyTestClassDI', ClassWithoutConstructor::class);
$container->set('ClassDIWithDependency', [SomeClass::class, 'message']);
$container->set('AnotherClassWithDIDependency', [TestClass::class, 'LoggerDI']);
Get your component in code
$logger = $container->get('LoggerDI');
Get your closure with arguments
$container->set('ClosureDI', function ($x, $y) { return $x + $y;});
$logger = $container->getWithParameters('ClosureDI', [1, 2]); // will return 3
$ composer test
The MIT License (MIT). Please see License File for more information.