jopic / jdi
simple dependency injection framework
Requires
- doctrine/common: *
- hafriedlander/phockito: 1.0.*
Requires (Dev)
- phpmd/phpmd: @stable
- phpunit/phpunit: 4.0.*
This package is auto-updated.
Last update: 2024-11-15 22:24:52 UTC
README
This repository provides a very basic and easy to use dependency injection "framework" for PHP.
Installation
You basically only need to add the following dependency "jopic/jdi": "1.0.0"
into your composer.json
file.
Usage - Container Setup
At first you have to set up your dependency container:
$container = new Jopic\DI\Container();
Then you can set this container as the active container for dependency injection by:
\Jopic\DI\DependencyInjection::getInstance()->setContainer($container);
Usage - Container Object registration
To be able to inject objects to class fields the dependency injection container needs to know about injectable objects. Therefore you register them on the container by calling the register
method.
$container->register("foo", function() {
return new DummyObject();
});
Please note: it is important to register a closure function (needed for object instantiation).
Usage - Object Injection
Basically the only 3 things you need to do are:
- extend the abstract
JDIBaseClass
- call
parent::__construct($this);
in your class constructor - annotate inject fields of your class with
@inject
Here is a short example of a Injectable class:
class SampleClass extends Jopic\DI\JDIBaseClass {
/**
* @inject
*/
private $foo;
public function __construct() {
parent::__construct($this);
}
}
This code assures that if a object with the name "foo" is registered in the dependency injection container it will be available in this class instances automatically.
Lazy Injection
If you define a property as protected, the class constructor of JDIBaseClass will automatically inject this property lazily. That means that the property closure gets executed just before the first property usage.
Here is a short example of a class with directly injected and lazily injected properties:
class SampleClass extends Jopic\DI\JDIBaseClass {
/**
* this value gets injected on constructor call
* @inject
*/
private $foo;
/**
* this value gets injected just before the first getFoo2() call
* @inject
**/
protected $foo2;
public function __construct() {
parent::__construct($this);
}
public function getFoo() {
return $this->foo;
}
public function getFoo2() {
return $this->foo2;
}
}
Request for comment
If you find any bugs or if you find something (or everything ;-) ) inconvenient please don't hesitate to contact me either directly via github or via e-mail (admin [at] jopic.at).