symfony DIC 3.0 prototype services

雨燕双飞 提交于 2019-12-14 02:38:46

问题


Prototype service means service, which is passed as dependency as always new fresh instance. In the end its similar to cloning the dependency in instance where its required, but its clean solution.

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

How can i set prototype services in Symfony DIC 3.0 configuration? (I prefer yml)


回答1:


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

Taken from the upgrade file....

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

Before:

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>

After:

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>


来源:https://stackoverflow.com/questions/31068052/symfony-dic-3-0-prototype-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!