0
votes

I'm trying to inject an object into one of my services, as well as another service. The second argument is just an object, however when declaring it in the services.yml file, I receive an exception staying that its expecting an object, but a string was given.

services.yml

parameters:
    app.my_class.class: AppBundle\MyClass
    app.object_to_inject.class: AppBundle\InjectObject

services:
    app.my_service:
        class: %app.my_class.class%
        arguments: [@app.another_service, %app.object_to_inject.class%]

Which results in:

Catchable Fatal Error: Argument 2 passed to AppBundle\MyClass::__construct() must be an instance of AppBundle\InjectObject, string given

I've tried quoting, unquoting and using new lines instead of square brackets:

arguments
    - @app.another_service
    - %app.object_to_inject.class%
1

1 Answers

1
votes

Parameters are just strings, so you you trying to inject the string of your class name, not the actual class.
You need to define AppBundle\InjectObject as a service, then inject that.

Example:

parameters:
    app.my_class.class: AppBundle\MyClass
    app.object_to_inject.class: AppBundle\InjectObject

services:
    app.my_object:
        class: "%app.object_to_inject.class%"
    app.my_service:
        class: %app.my_class.class%
        arguments: [@app.another_service, @app.my_object]