Symfony:将对象(不是服务)注入服务构造函数

Jon*_*ram 10 symfony

在我的服务定义中,我想作为服务参数构造函数传递一个对象而不是一个服务.

来自config.yml:

services:
  acme.services.exampleservice:
    class:  Acme\ExampleBundle\Services\ExampleService
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"
        httpClient: \Example\Http\Client\Client
Run Code Online (Sandbox Code Playgroud)

注意这个httpClient论点.这必须是\Example\Http\Client\Client类的实例.

以上操作无效 - 字符串"\ Example\Http\Client\Client"作为httpClient参数传递给服务.

通过将实例传递\Example\Http\Client\Client给服务构造函数来实现上述目的的语法是什么?

gil*_*den 19

创建私人服务.这是文档的内容:

如果您使用私有服务作为多个其他服务的参数,这将导致使用两个不同的实例,因为私有服务的实例化是内联完成的(例如,新的PrivateFooBar()).

services:
  acme.services.exampleservice:
    class:  Acme\ExampleBundle\Services\ExampleService
    arguments: 
        entityManager: "@doctrine.orm.entity_manager"
        httpClient: acme.services.httpClient

  acme.services.httpClient:
    class: Example\Http\Client\Client
    public: false
Run Code Online (Sandbox Code Playgroud)

您将无法从容器中检索私人服务.从外部看起来就像是将常规对象传递给服务的构造函数.