Skip to content
Advertisement

Symfony: Service in other Service not loaded (Too few arguments)

Background

I have a OptionHelper-Class that is looking in the database-table, where I store some flexible parameters. This class is working fine in Controllers.

Now I want to use this class in a function of another Service, but it explodes with the below mentioned error message.

OptionHelper.php

JavaScript

CartItem.php

JavaScript

Controller

JavaScript

Error-Msg

JavaScript

Question

Why isn’t it working in another Service? I tried also to put it into the constructor of CartItem-Class, but doesn’t work too.

Do I have to add something in services.yaml? But I have no idea, how to do it.

Thanks in advance for any help!

Advertisement

Answer

After a while I found the problem. It seems that you should never do this, because it disables the autowiring functionality.

  • use new ClassName($x, $y) to create a new instance
  • only use autowirable Services in the constructor of a class

wrong

JavaScript
JavaScript

correct

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