dev-master
9999999-dev http://github.com/peec/dependencyinjectorDependency Injection Container.
MIT
The Requires
- php >=5.4.0
Wallogit.com
2017 © Pedro Peláez
Dependency Injection Container.
Simple dependency injection container (DIC) or otherwise known as Inversion of Control (IoC). DIC helps you sort out dependencies for objects in a nice manner., (*1)
$di = new DependencyInjector(); class World { private $str; public function __construct($str) { $this->str = $str; } public function __toString () { return $this->str; } } $di->service('world', function ($string) { return new World($string); }, ['World']); $di->service('test', function ($number, $thing) { return "Hello $thing, this is a number: $number!"; }, [1337, '@world']); echo $di->service('test');
Outputs:, (*2)
Hello World, this is a number: 1337!
Use the service method to create new services., (*3)
Argument 2's arguments. Use a string starting with @ to inject a service, otherwise you can inject anything. Note that everything starting with a @ is treated as a service.It's possible to configure the DependencyInjector with the config method. You can also define custom global settings using ->config($key, $value)., (*4)
Listed configuration below:, (*5)
For convenience you can set DependencyInjector::CONF_STATIC_ANALYSIS to true, by doing this you don't need to specify arguments in the third argument using the service method., (*6)
Example:, (*7)
// Enable static anlysis using reflection.
$di->config(DependencyInjector::CONF_STATIC_ANALYSIS, true);
// The following code:
$di->service('test', function ($world) {
return "Hello $world!";
}, ['@world']);
// Can now become:
$di->service('test', function ($world) {
return "Hello $world!";
});
Dependency Injection Container.
MIT