I'm developing php application with Doctrine (no symfony) and hence no DI container. I'm using dependency Injection in one of my entities, which needs a service.
Class A implements Ia
{
public function __constructor( equiredServiceClass $requiredService = NULL )
{
if ($requiredService === NULL) {
$this->requiredService = new equiredServiceClass();
{
$this->requiredService = $requiredService;
}
}
}
Every thing works fine but while hydrating Doctrine doesn't call the __constructor as a result the dependency is not injected. What's the best way to solve this?
Currently I use Doctrine lifecycle events to callback a method to set the dependency. So first I add the lifecycle-callbacks in the mapping file of the entity
<lifecycle-callbacks>
<lifecycle-callback type="postLoad" method="setRequiredService"/>
</lifecycle-callbacks>
And then in the called method inject the dependency applying the setter dependency Injection.
public function setRequiredService()
{
$this->requiredService = new equiredServiceClass();
}
My Questions: Is this the best way to solve dependency Injection while hyderation in Doctrine? And is it fine to pass DI param with default as NULL?
Thanks, Abhinit Ravi