简体   繁体   English

在Symfony2中将Array的单个元素用作服务构造函数参数

[英]Using Single element of Array as service constructor argument in symfony2

In the symfony2 docs at http://symfony.com/doc/current/components/dependency_injection/parameters.html it states that parameters (in service definitions) can be defined as arrays eg: http://symfony.com/doc/current/components/dependency_injection/parameters.html上的symfony2文档中,它指出(在服务定义中)参数可以定义为数组,例如:

parameters:
    foo.class:"Foo"
    foo.args:
        - arg1
        - arg2

I want to know how to pass a single element of the foo.args array to the service constructor ie: 我想知道如何将foo.args数组的单个元素传递给服务构造函数,即:

services:
    foo:
        class:"%foo.class%"
        arguments:["%foo.args.1%"] //this line is in question.does this resolve to
                                   //"arg1"?if not, what does?

Your current implementation won't work. 您当前的实现无效。

Internally symfony would try to access array['1'] (key=string) instead of array[1] (key=int). 在内部,symfony将尝试访问array['1'] (key = string)而不是array[1] (key = int)。

Consider using a service factory that in turn creates the service with the desired element of the array as argument ... 考虑使用服务工厂 ,该服务工厂依次使用数组的所需元素作为参数来创建服务...

... or just filter the array elements in the constructor of the class. ...或者只是在类的构造函数中过滤数组元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM