The share of Pimple DI has depreciated. Now what?

In Pimple 1.0, I used to share class instances as follows:

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

Now this seems deprecated and I cannot find that this is a new way to do this.

+6
source share
3 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(); }); 
+11
source

It seems that by pimple 3.0 (which uses Silex 2.0), by default it always returns the same service instance. You need to be explicit and use the factory function if you do not want this behavior.

+1
source

Depends on the version of Pimple!

In Pimple 1.0

 $container['shared'] = $container->shared(function(){ return new Class(); }); $container['non_shared'] = function() { return new Class(); }; 

In Pimple 3.0

 $container['shared'] = function() { return new Class(); }; $container['non_shared'] = $container->factory(function() { return new Class(); }); 

Remembering that when they create a shared service, what they return will not change. When you create a non-shared service, each time you use Pimple will provide you with a new instance of this service.

+1
source

All Articles