Minimal, Lightweight, and Fast PHP Dependency Injection Container
composer require redot/container
composer test
The methodologies of the container are based on the Dependency Injection paradigm, using the ReflectionClass
to get class dependencies.
To use the container, you must first create a new instance of the container.
use Redot\Container\Container;
$container = new Container();
Or just use the static method Container::getInstance()
, that will return the globally available container if it exists, or create a new one.
$container = Container::getInstance();
After you have created the container, you can bind your dependencies to the container.
$container->bind(Foo::class);
Also, you can create a singleton, that will be returned every time you call the get
method.
$container->singleton('foo', function () {
// ...
});
Singletons are useful for classes that are expensive to instantiate, but only need to be created once.
To get a dependency from the container, you can call the make
method.
$foo = $container->make('foo');
The main difference between make
and get
is that make
accepts a second parameter, which is an array of parameters to pass to the constructor of the class, while get
does not because of implementing PSR-11
By the way you can also create an alias for a class, so you can call it with a different name.
$container->alias(Foo::class, 'bar');
Don't worry about the auto-wiring, the container will do it for you.
$container->make(BrandNewClass::class);
It will automatically bind the dependencies of the class, and if the class has a constructor, it will pass the dependencies to it, also the container can inject specific method dependencies using the call
method.
$container->call([Foo::class, 'setBar'], ['bar' => $bar]);
And that's it! Enjoy ✌.