People do not want to use constructors because of too much code, it's much easier to write
Yii::$app->component
In 2018 year it looks very bad = (
public function __construct(string $id, Module $module, array $config = [], ClientManager $manager)
{
$this->manager = $manager;
parent::__construct($id, $module, $config);
}
| Q | A
| ---------------- | ---
| Yii version | 3.0
| PHP version | any
| Operating system | any
Please remove your code from the constructor, leave a clean constructor for developers.
Can you elaborate?
Having code and parameters in constructors is a good thing in my opinion, it clearly defines the dependencies of the code.
(I'm not talking about the array $config param)
@SamMousa What are the parameters?
"string $id, Module $module, array $config = []"
Completely superfluous, and not needed to write your own code
So how do we get them into the base class who does need them?
@SamMousa Any other methods. This works in symfony, laravel. If you need the Module $module for the user, then ok, let it add to the constructor.
But, These 3 parameters should not be mandatory for the developer.
For example, look at the more popular frameworks, they all have pure constructors. They hide all the inner magic, that we would write a cleaner code
yii2
//PostController.php
public function __construct(string $id, Module $module, array $config = [], Service $service)
{
$this->service = $service;
parent::__construct($id, $module, $config);
}
Other framework
//PostController.php
public function __construct(Service $service)
{
$this->service = $service;
}
So if our controller needs to know to which module it belongs, how would we do that?
I honestly don't see the problem, any decent IDE will insert boilerplate for calling the parent constructor.
I'm not against changing the structure of the framework (why does a controller need to know its parent?) but the reason in my opinion should not be to clean up constructors...
FWIW:
Within the current structure of Yii having empty constructors is simply not possible.
If you have specific improvements in mind that as a side effect lead to things like empty constructors feel free to propose them.
If you want to know about the module in the controller. just write
public function __construct(Module $module)
Next DI will do everything himself. I just want DI to do everything that you need. but the framework did not make me write unnecessary arguments. and did not clog my code.
To invoke LoggerInterface I need to write 3(!) additional arguments in the constructor.
So if our base controller needs to know to which module it belongs we add Module to the arguments?
Which you as a developer must then pass on via the parent::__construct call, which is exactly what we have now...
Let YII provide Module not through the constructor.
I just want DI to be used 100%. but not as now.
Developers prefer the service locator instead of DI, and this is very bad.
Code sample? Providing dependencies via constructor is what DI is all about; if our controller depends on the module we should pass it in via the constructor...
Closing because we're going to have it this way in 3.0 in almost all places.
Most helpful comment
Code sample? Providing dependencies via constructor is what DI is all about; if our controller depends on the module we should pass it in via the constructor...