Skip to content
Advertisement

Instantiate new class instance inside Symfony services (Best practice)

In Symfony when I create a service and I need an instance of some classes, I pass them as parameter in service or I can create a new instance inside my functions service. I ask this because some Classes constructor needs parameters which I don’t have yet when passing the class as parameter.

Advertisement

Answer

you pass them as params in service. try to inject your deps to the max and not instantiate directly your services.

see SOLID development principle and particulary in this case Dependency Inversion Principle

enter image description here

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement