di\Container invoke()

invoke() public method (available since version 2.0.7)

Invoke a callback with resolving dependencies in parameters.

This methods allows invoking a callback and let type hinted parameter names to be resolved as objects of the Container. It additionally allow calling function using named parameters.

For example, the following callback may be invoked using the Container to resolve the formatter dependency:

$formatString = function($string, \yii\i18n\Formatter $formatter) {
   // ...
}
Yii::$container->invoke($formatString, ['string' => 'Hello World!']);

This will pass the string 'Hello World!' as the first param, and a formatter instance created by the DI container as the second param to the callable.

public mixed invoke ( callable $callback, $params = [] )
$callback callable

Callable to be invoked.

$params array

The array of parameters for the function. This can be either a list of parameters, or an associative array representing named function parameters.

return mixed

The callback return value.

throws yii\base\InvalidConfigException

if a dependency cannot be resolved or if a dependency cannot be fulfilled.

throws yii\di\NotInstantiableException

If resolved to an abstract class or an interface (since 2.0.9)

doc_Yii
2016-10-30 17:01:05
Comments
Leave a Comment

Please login to continue.