palmic
palmic

Reputation: 1856

symfony DIC 3.0 prototype services

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)

Upvotes: 1

Views: 772

Answers (1)

qooplmao
qooplmao

Reputation: 17759

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>

Upvotes: 4

Related Questions