Answer the question
In order to leave comments, you need to log in
How to properly inject Yii2 class dependencies?
Help me understand where I'm wrong. It seems like I'm doing everything right!
We need to extend the functionality of the class:
use custom\class\Finder;
class BaseLoginForm extends Model
{
/** @var Finder */
protected $finder;
/**
* @param Finder $finder
* @param array $config
*/
public function __construct(Finder $finder, $config = [])
{
$this->finder = $finder;
parent::__construct($config);
}
}
use own\class\Finder;
class LoginForm extends BaseLoginForm
{
/** @var Finder */
protected $finder;
/**
* @param Finder $finder
* @param array $config
*/
public function __construct(Finder $finder, $config = [])
{
$this->finder = $finder;
parent::__construct($finder, $config);
}
}
Finder
the class needs to be registered somewhere so that it Yii
knows where to inject the dependency ????? Help me to understand !
Answer the question
In order to leave comments, you need to log in
In the child class, it is not necessary to describe finder
the constructor either, because they are inherited.
Concerning implementation: whence Yii2 should take a class Finder
?
And in fact, the following code is also an injection:
$finder = new Finder();
$loginForm = new LoginForm($finder);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question