即使服务缺少依赖性,服务也不会被忽略

时间:2016-10-18 13:16:21

标签: php symfony dependency-injection

我有一个服务定义,如下所示:

app.logging.event_subscriber.my_subscriber:
    class: AppBundle\Event\Subscriber\MySubscriber
    arguments:
        - '@?app.logging.optional_dependency'
    tags:
        - { name: kernel.event_subscriber }

如果缺少“app.logging.optional_dependency”服务,则不应定义我的服务“app.logging.event_subscriber.my_subscriber”,但我得到例外:

TypeError: Argument 1 passed to AppBundle\Event\Subscriber\MySubscriber::__construct() must be an instance of AppBundle\Logging\OptionalDependency, null given, called in /app/app/cache/test/appTestProjectContainer.php on line 936

1 个答案:

答案 0 :(得分:2)

为构造函数null

的参数添加默认值AppBundle\Event\Subscriber\MySubscriber

public function __constructor (OptionalDependency $dependency = null)