JMSDiExtraBundle
JMSDiExtraBundle copied to clipboard
Inject and private/public incoherence
I found that really confusing :
/**
* @Inject
*/
private $session;
But it's also said : Note that you cannot use the @Inject annotation on private, or protected properties. Likewise, the @InjectParams annotation does not work on protected, or private methods.
In all the different cases (Controller / not controller / public / private, etc...), in the end its gets really confusing to use the annotations.
So basically you can inject a service into a private method, but you CANT inject a param into a private method... (or i am mistaken ?)
NB: what i was trying to do is inject a simple parameter in a Controller defined as a service.
if it is not an controller you will have to use public property.
for controllers you can use private properties
@adadgio the difference is that this bundle actually replaces your controllers by special classes generated with AOP to enable such extra features. This is possible because it replaces the service instantiating controllers. But for services, it would be much harder to do it
and btw, if your service does not have any way to inject the parameter in it through its public API (i.e. without using hacks to access the internals of the object), it is not actually using dependency injection (as there is no way to inject the dependencies).
What about setters?
class MyClass {
/**
* @var RouterInterface
* @Inject("router")
*/
private $router;
/**
* @param RouterInterface $router
* @return $this - Provides Fluent Interface
*/
public function setRouter(RouterInterface $router) {
$this->router = $router;
return $this;
}
}
IMO, the @Inject annotation is easier to read (as a human) on properties. Maybe the library should make use of setters (usually a private $my_router property should have a public setter setMyRouter()) Or with Reflection?