Browse Source

documentation about method injection

fixes #11565
tags/2.0.9
Carsten Brandt 8 years ago
parent
commit
1423e34131
  1. 31
      docs/guide/concept-di-container.md

31
docs/guide/concept-di-container.md

@ -13,6 +13,7 @@ Yii provides the DI container feature through the class [[yii\di\Container]]. It
dependency injection:
* Constructor injection;
* Method injection;
* Setter and property injection;
* PHP callable injection;
@ -39,6 +40,36 @@ $foo = new Foo($bar);
```
### Method Injection <span id="method-injection"></span>
Usually the dependencies of a class are passed to the constructor and are available inside of the class during the whole lifecycle.
With Method Injection it is possible to provide a dependency that is only needed by a single method of the class
and passing it to the constructor may not be possible or may cause too much overhead in the majority of use cases.
A class method can be defined like the `doSomething()` method in the following example:
```php
class MyClass extends \yii\base\Component
{
public function __construct(/*Some lightweight dependencies here*/, $config = [])
{
// ...
}
public function doSomething($param1, \my\heavy\Dependency $something)
{
// do something with $something
}
}
```
You may call that method either by passing an instance of `\my\heavy\Dependency` yourself or using [[yii\di\Container::invoke()]] like the following:
```php
$obj = new MyClass(/*...*/);
Yii::$container->invoke([$obj, 'doSomethingWithHeavyDependency'], ['param1' => 42]); // $something will be provided by the DI container
```
### Setter and Property Injection <span id="setter-and-property-injection"></span>
Setter and property injection is supported through [configurations](concept-configurations.md).

Loading…
Cancel
Save