How to return new instance from container on every call, like Pimple::factory()

One of the features of Pimple is using the factory() method on the container (see https://pimple.symfony.com/#defining-factory-services ), which tells it to call the given closure every time that item is requested. This allows you to have a dependency that returns a new instance of a class every time it is called (instead of the default behavior of sharing the same instance of a dependency every time it is called from the container).

It does not look like the Slim DIC implements this functionality, but is there a way to emulate it somehow? Or to somehow “pass through” to the underlying Pimple container to utilize this functionality?

If none of that is possible, could this be something added to the framework in a future version? Or has this been intentionally left out for some reason?

Thanks!

Slim’s Container extends Pimple\Container, so you can call factory() on it.

1 Like