The EncorePHP Container library provides a fast and powerful Dependency Injection Container for your application.
As well as a dependency injection container, we also allow the use of service providers and proxies.
Via Composer
$ composer require encorephp/container
require 'vendor/autoload.php';
$container = new Encore\Container\Container;
$container->bind('\Foo\Bar\BazInterface', '\Foo\Bar\Baz');
The Container is able to recursively resolve objects and their dependencies by inspecting the type hints on an object's constructor.
namespace Foo\Bar;
class Baz
{
public function __construct(Qux $qux, Corge $corge)
{
$this->qux = $qux;
$this->corge = $corge;
}
public function setQuux(Quux $quux)
{
$this->quux = $quux;
}
}
$container->resolve('\Foo\Bar\Baz');
Alternatively, you can specify what to inject into the class upon instantiation.
$container->bind('\Foo\Bar\Baz')->addArgs(array('\Foo\Bar\Quux', '\Foo\Bar\Corge'));
$container->resolve('\Foo\Bar\Baz');
$container->bind('\Foo\Bar\Baz')->withMethod('setQuux', array('\Foo\Bar\Quux'));
$container->resolve('\Foo\Bar\Baz');
Another great feature of the the container is the ability to resolve the dependencies of inherited classes and interfaces. For example, you could bind an interface into the container which requires a dependency injected via a method, and all classes that implement that interface will also have that dependency injected automatically.
$container->bind('\Foo\Bar\BazInterface')->withMethod('setQuux', ['\Foo\Bar\Quux']);
$container->bind('\Foo\Bar\Baz');
$container->resolve('\Foo\Bar\Baz');
A great feature of the container is it's ability to provide child containers with a separate resolution scope to that of it's parent container. If you bind a concrete class to an interface within one container, you can re-bind it in the child container, without fear of overwriting the original binding in the parent container.
There are two ways to create a child container.
$child = $continer->createChild();
// OR
$child = new Container($container);
The primary benefit of using child containers is scope-specific resolution.
$container->bind('FooInterface', 'Foo');
// Assuming class Bar has a FooInterface dependency.
// This would use the Foo implementation.
$bar = $container->resolve('Bar');
// ...
$child = $container->createChild();
$child->bind('FooInterface', 'Baz');
// And this would use the Baz implementation.
$bar = $child->resolve('Bar');
$ phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email chris@chrisnharvey.com instead of using the issue tracker.
The EncorePHP Container is based onthe deprecated league/di package.
The MIT License (MIT). Please see License File for more information.