When do I need FactoryInterfaces?

Hey there,

I have a question, I am learning mezzio and programming in general for no long time. So I had the task to create some factorys in my code. A co-worker showed me how and one time he implemented the FactoryInterface and one time he doesn’t. Do I have to use the FactoryInterface for every Factory? Because I did not use it in every Factory and it works. I could not figure out, for what exactly I need the FactoryInterface and for what not.

Can somebody explain it to me?

kind regards,
hjerti

Hello and welcome to our forums! :smiley:

If you mean Laminas\ServiceManager\Factory\FactoryInterface then no. There are two reason:

  1. The interface comes with laminas-servicemanager but you can use other dependency injection containers. This means that the interface is not always available.
  2. A factory does not have to be a class.
1 Like

thank you for the welcome!
I think that helps, thanks :slight_smile: