6

This is more abstract question then specific problem.

Why auto-generated factories in magento do not implements any common interface?

And what bothers me maybe more. Why auto-generated factories doesn't extend each other, so they could be properly passed as arguments.


For example. I have class MagicClass. Then I make a class class MoreMagicClass extends MagicClass.

By my opinion auto-generated MoreMagicClassFactory should extend MagicClassFactory.

So when i have another class with constructor that looks like this __construct(MagicClassFactory $magicClassFactory), I could pass the childs factory as well.

I would like to make that as a feature request, but I suspects, there is a good reason, why the auto-generated factories does not behave the way i would like to.

So, is there a reason?


EDIT:

I made feature request on Magento official forum.

4

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.

3

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 MoreMagicClassFactory.
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.

  • "...you can build your own as it fits your needs." Yes, i know that, but i don't thinks it's good doing so, only to solve this problem. – michalhosna Mar 16 '17 at 15:59
  • If you need it for a specific case, it makes sense. Otherwise I don't see why the factories should know anything about each other. Maybe the interface implementation. You might be right there. – Marius Mar 16 '17 at 16:00
  • But i still don't se why factories should not know about each other. Why should factories be that dumb. Does it break some design pattern, some norm? – michalhosna Mar 16 '17 at 16:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.