5 ms·
>> There is a second form of Dependency Injection that uses setters instead of constructor injection. Do not use this form. >Why? Not using setter injection el
by Danack 11y ago
>> There is a second form of Dependency Injection that uses setters instead of constructor injection. Do not use this form.
>Why?
Not using setter injection eliminates a huge class of bugs that occur when people try to use objects that haven't had all of their dependencies injected yet:
$user = new RegisteredUser();
// Any code that touches $user here will be bad
$user->setEmail($email);
> constructor injection can become a nightmare.
Which is why people use library to do it for them - an example for PHP https://github.com/rdlowrey/auryn https://github.com/rdlowrey/auryn.