Dependency (Constructor) Injection With Constructor Parameter

I want to pass some config parameters to a Component which is "constructor injected" inside of another base component ("myBaseComponent").

Example:

Component interface to be as flexible as possible cause changing a complete component implementation have to be possible.


interface myComponentInterface

{

  function myFind();

}

A sample component implementation:


class myComponent extends Component implements myComponentInterface

{

  private $_param1;

  private $_param2;


  public function __construct($param1, $param2, $config = [])

  {

	$this->_param1 = $param1;

	$this->_param2 = $param2;

	parent::__construct($config);

  }


  public function myFind()

  {

	// do something

  }

}

A base component where myComponent is injected via constructor:


class myBaseComponent extends Object // or extends Component if I need events, don't know yet

{

  public $myComponent;


  /*

   * inject component via constructor by passing Interface as parameter

   */

  public function __construct(myComponentInterface $myComponent, $config = [])

  {

	$this->myComponent = $myComponent;

	parent::__construct($config);

  }

}

The following script is included in the init yii script:




\yii\di\Container->setSingleton('app\components\myComponentInterface', 'app\components\myComponent', [

  'param1' => $params['myComponent']['param1'], // values are configured in params array in params.php

  'param2' => $params['myComponent']['param2'],

]);

The dependency injection via constructor is working fine but I was not able to init/construct MyComponent with the passed parameters given as 3rd parameter to Container->setSingleton.

What I want:

Pass some configuration parameters to the dynamic injected myComponent so I can configure Objects/Components like the section Object Configuration of Basic concepts of Yii guide says.

Components should be accessible like


Yii::$app->myBaseComponent->myComponent->myFind

Do I think and/or code wrong or is it maybe a not (yet?!?) supported feature or bug of Yii 2.0?

Can someone please provide me a working example how I can achive my goal, thanks.

Another related question/problem:

The DI guide says that constructor, setter and php callable injection is possible.

There is no setter injection example in the guide and I couldn’t figure out how it can be done the yii way.

Maybe someone can adopt my example above to setter injection, thanks.

I think the guide should also provide an extra setter inection example so it’s more clear how to use.

If more guide examples are already planned for stable release then forget it ;)

no one?

Yesterday I tried again and I found out that it works only with number-indexed instead of name-indexed constructor parameter array as 3rd parameter.

The DI configuration has to change from




\yii\di\Container->setSingleton('app\components\myComponentInterface', 'app\components\myComponent', [

  'param1' => $params['myComponent']['param1'], // values are configured in params array in params.php

  'param2' => $params['myComponent']['param2'],

]);



to




\yii\di\Container->setSingleton('app\components\myComponentInterface', 'app\components\myComponent', [

  0 => $params['myComponent']['param1'], // values are configured in params array in params.php

  1 => $params['myComponent']['param2'],

]);



Question is now if this is a bug or a wanted behaviour?

I’m going to make test for it and create a bug report on github.

I’ll keep you up to date ;)

It’s 2021 now, and I still found this behaviour. How is this supposed to be? Numbered parameters would be quite a PITA in my case.