Allow @inject into private and protected properties

Sitole
Member | 39
+
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.

CZechBoY
Member | 3608
+
+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;
	}
}
Sitole
Member | 39
+
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.

CZechBoY
Member | 3608
+
0
-

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