I have been practicing constructor dependency injection throughout my PHP application. I didn’t want to be littering my code with object creation, so factories to the rescue, or at least I thought.
I set about wiring up components with factories, then some factories started using other factories to get dependencies, great, keeps all the creation code in one place. However, once factories start using each other (or as in the code below, itself) I ran into circular dependency issues, that simply cannot be resolved. For example, my MapperFactory uses itself to inject mappers with other mappers (they need each other to build a full object graph ‘eager loading’):
class MapperFactory
{
public function create($type)
{
switch (true) {
case 'Item':
$mapper = new ItemMapper(
$this->create('Field')
);
break;
case 'Field':
$mapper = new ItemMapper(
$this->create('Item')
);
break;
default:
throw new Exception('Unknown mapper');
}
return $mapper;
}
}
$mf = new MapperFactory();
$mf->create('Item');
Its a simplified example, but an increasingly common issue as the application is developing. Error back from PHP (xdebug installed) is:
Fatal error: Maximum function nesting level of '100' reached, aborting!
Fully understand why PHP is complaining (although didn’t see it coming TBH).
My question is, have I completely missed the point of factories? am I using factories correctly? It would seem not, but other than the circular dependency (pretty major but), factories are an elegant solution to hiding all the construction/wiring logic away from the main application.
You could try using a setter for injecting the dependencies. Then you’d create both mappers like this:
And then use the switch just to return the mapper. This should get rid of the circular dependencies when creating objects.
Having said that, if you are doing this as a sort of OR/M thing to connect to a database, you should maybe look into stuff like Doctrine2 or Propel, just to save yourself the trouble of inventing the wheel when there are already tried and tested solutions out there.