Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pimple DI share deprecated. Now what?

In Pimple 1.0 I used to be able to share class instances like this:

$app['some_service'] = $app->share(function () {
    return new Service();
});

This now seems to be deprecated and I am unable to find what is the new way of doing this.

like image 506
Adam Avatar asked Jan 12 '16 19:01

Adam


2 Answers

In Pimple 1.0 (Silex 1), you do this:

$app['shared_service'] = $app->share(function () {
    return new Service();
});

$app['non_shared_service'] = function () {
    return new Service();
};

In Pimple 3.0 (Silex 2) you do this (which is the opposite!):

$app['shared_service'] = function () {
    return new Service();
};

$app['non_shared_service'] = $app->factory(function () {
    return new Service();
});
like image 87
Javier Eguiluz Avatar answered Sep 20 '22 10:09

Javier Eguiluz


Seems that by pimple 3.0 (which Silex 2.0 uses) by default always returns the same instance of the service. You need to be explicit about it and use the factory function if you don't want this behaviour.

like image 22
Adam Avatar answered Sep 22 '22 10:09

Adam