7

I would like to inject interface app\models\IFoo

private $foo;

public function __construct($id, $module, IFoo $foo, array $config = [])
{
    parent::__construct($id, $module, $config);
    $this->foo = $foo;
}

How should I set container to correct resolving the dependency in concrete class app\models\Foo which implements IFoo?

2
  • Can you explain what is the purpose of this? Might be easier to answer. Commented Jan 2, 2017 at 9:02
  • I've implemented IFoo in two concrete classes: Foo and FooStub. I would like to use stub for testing and so on. And I want inject interface into controller and set concrete class in setup file (web.php or other). Commented Jan 2, 2017 at 12:19

1 Answer 1

3

Try to call it like that in entry script:

\Yii::$container->set('app\models\IFoo', $concreteClass);

where $concreteClass is either 'app\models\Foo' or 'app\models\FooStub'.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess you can add it in web.php for non-test env and in test.php for test env.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.