niletphp / dependency-container
The NiletPHP dependency injection container.
Installs: 151
Dependents: 2
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
pkg:composer/niletphp/dependency-container
Requires
- php: >=7.0
Requires (Dev)
- phpunit/phpunit: ~5.7
This package is not auto-updated.
Last update: 2025-09-28 06:49:04 UTC
README
Dependency container which provides an easy way for managing dependencies in your PHP projects.
Requirements
PHP >= 7.0
Install
Composer
{ "require": { "niletphp/dependency-container": ">=v1.0" } }
Examples
$dc = new Nilet\Components\Container\DependencyContainer();
Resolve dependencies for a given concrete and return a new instance of it
$dc->create("Nilet\Foo");
Bind a concrete to interface. Resolving such binding will always return new concrete instance
$dc->bind("Nilet\FooInterface", "Nilet\Foo"); $dc->bind("Nilet\FooInterface", function ($dc) { return new Nilet\Foo(); }); $dc->create("Nilet\FooInterface");
Share a concrete (singleton).
$dc->share("Nilet\Foo"); $dc->share("Nilet\Foo", function ($dc) { return new Nilet\Foo(); });
Binding a concrete (singleton) to an Interface
$dc->bindShared("Nilet\FooInterface", "Nilet\Foo"); $dc->bindShared("Nilet\FooInterface", function ($dc) { return new Nilet\Foo(); });
Register a concrete instance (singleton).
$foo = new Nilet\Foo(); $dc->instance("Nilet\Foo", $foo);
Retrieve a concrete.
$dc->get("Nilet\Foo"); $dc->bindShared("Nilet\FooInterface", "Nilet\Foo"); $dc->get("Nilet\FooInterface");
Determine if a given concrete (singleton) has been resolved.
$dc->bindShared("Nilet\FooInterface", "Nilet\Foo"); $dc->get("Nilet\FooInterface"); if ($dc->isResolved("Nilet\FooInterface")) { // evaluates to true // do something } else { // do something else }
Determine if a given concrete is shared.
$dc->share("Nilet\Foo"); if ($dc->isShared("Nilet\Foo")) { // evaluates to true // do something } else { // do something else }
Determine if a given interface is bound.
$dc->bind("Nilet\FooInterface", "Nilet\Foo"); if ($dc->isBound("Nilet\FooInterface")) { //evaluates to true // do something } else { // do something else }
Determine if a given interface is bound/shared.
$dc->bindShared("Nilet\FooInterface", "Nilet\Foo"); if ($dc->isBoundShared("Nilet\FooInterface")) { //evaluates to true // do something } else { // do something else }