Skip to content

Commit

Permalink
documentation about method injection
Browse files Browse the repository at this point in the history
fixes #11565
  • Loading branch information
cebe committed Jul 11, 2016
1 parent bda42b6 commit 1423e34
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions docs/guide/concept-di-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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).
Expand Down

0 comments on commit 1423e34

Please sign in to comment.