Allow @inject into private and protected properties

5 months ago

Sitole
Member | 15
+
0
-

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.

5 months ago

CZechBoY
Member | 3096
+
+1
-

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;
    }
}

5 months ago

Sitole
Member | 15
+
0
-

@CZechBoY
Yes, i know.
But my question is, why is property injection allowed only for public properties.
Inject data into private property is big mistake for OOP integrity?

Sorry for my bad english.

5 months ago

CZechBoY
Member | 3096
+
0
-

yes, it is something magic that non visible property is filled outside class