Allow @inject into private and protected properties
- Sitole
- Member | 39
Hey,
in the last time i changing constructor DI property to @inject property but injecting is working only with public properties. Why is not injection allowed on private and protected properties? I know, inject something into object destroys OOP encapsulation but its smart way because only framework will has access to inject and not anyone else. For first time i thought that PHP don´t support gets a list with names of private and protected properties inside object but it's not true. What is reason of that?
Sorry for my bad english.
- CZechBoY
- Member | 3608
You can use inject* methods, which are more clean than inject properties.
They are automated setters to be accurate.
btw. you should prefer constructor injection against other inject
methods/properties. Only abstract parents should have inject methods.
Example of inject methods
class MyPresenter extends Nette\Application\UI\Presenter
{
private $myService;
public function injectMyService(MyService $myService)
{
$this->myService = $myService;
}
}