symfony DIC 3.0原型服务 [英] symfony DIC 3.0 prototype services

查看:42
本文介绍了symfony DIC 3.0原型服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原型服务是指服务,它作为依赖项始终作为新的新实例传递.最后,它类似于在需要的情况下克隆依赖关系,但解决方案却很干净.

根据Symfony新闻Twitter 上写的内容,范围已正式弃用.原型服务是根据作用域设置的.

As writen on Symfony news Twitter, scopes was officially deprecated. prototype services was set up by scopes.

如何在Symfony DIC 3.0配置中设置原型服务?(我更喜欢yml)

推荐答案

通过查看升级2.7至2.8 表示 scope:prototype 标志已更改为 shared:false .

From looking at the upgrade 2.7 to 2.8 it says that the scope: prototype flag has been changed to shared: false.

来自升级文件....

已将新的共享标志添加到服务定义中,以代替原型作用域.

A new shared flag has been added to the service definition in replacement of the prototype scope.

之前:

use Symfony\Component\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();
$container
    ->register('foo', 'stdClass')
    ->setScope(ContainerBuilder::SCOPE_PROTOTYPE)
;

services:
    foo:
        class: stdClass
        scope: prototype

<services>
    <service id="foo" class="stdClass" scope="prototype" />
</services>

之后:

use Symfony\Component\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();
$container
    ->register('foo', 'stdClass')
    ->setShared(false)
;

services:
    foo:
        class: stdClass
        shared: false

<services>
    <service id="foo" class="stdClass" shared="false" />
</services>

这篇关于symfony DIC 3.0原型服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆