Why auto generated Factories do not extends or implements any classes (interfaces)

Adding to Marius answer when you look at those auto-generated factories code it is always only call of \Magento\Framework\ObjectManagerInterface::create(). There is no more to it. Making it extend each other to some abstract factory would mean that those auto-generated classes would be completely empty or having one property with class name to instantiate.

You can also look at the code that currently generates Factory classes, it's in Magento\Framework\ObjectManager\Code\Generator\Factory. Adding parent classes there would require some additional complications as you would either have to generate them recursively which can be dangerous as php can have xdebug with max nesting level set. If not recursively then you would generate code for class ChildFactory and when try to load it went again to code generator to generate code for ParentFactory and so one. Since magento classes tend to have large ancestry line I suspect this process would extend di:compile noticable. In production mode this would be only done once in a while when you update the server but in development when you work on the code and do have to regenerate those classes frequently it could be pain in the ass to already slow system.

All in all applying inheritance to auto-generated factories would have not much benefit with significant larger costs of doing that.


I don't see any reasons for the factories of related classes to be related by themselves.
A factory has only one scope. Create other instances.
MagicClassFactory creates instances of MagicClass and MoreMagicClassFactory creates instances of MoreMagicClass.
They should not know more about the classes they instantiate. They should not care if the classes are related somehow.

For implementing an interface, I'm not sure about it. Maybe it makes sense for all of them to implement a FactoryInterface or something like that. I have no definitive answer/opinion on this.

But in addition, if you don't like for some reason the autogenerated factory, you can build your own as it fits your needs.
The factories are autogenerated only if the class does not exist.
So if you have the class MagicClassFactory written by you, Magento will not autogenerate one in var/generation.