In my app I use constructor injection, and I define it using \DI\ContainerBuilder::addDefinitions() method.
One of injected objects is injected as a factory, and this object is randomly configured in factory.
But when I get this object in constructors, the object is always same. Usually it is ok, but I would like to disable this behavior for single definition.
There is method \DI\Container::make(), but I don't know how to use it without passing container object to constructors where my randomly object needed.
Maybe there is any way to disable caching for single definition, or use make() instead of get() when constructor dependencies are created? Or should I refactor my application anyway?
The text was updated successfully, but these errors were encountered:
Here $foo1 and $foo2 are the same object, but you want a new object each time. Is this your problem?
There are 2 solutions:
The DI\factory definition doesn't allow to change the Scope -> it is fixable (it needs some code change though)
Use Container::make() as you said
I will have a look at the first solution: I'll try to add the option of "scope" to the "factory" definitions in the next release. It shouldn't be very hard. (i'll keep this issue open to track that new feature)
In the meantime, if you don't want to wait for that new feature, you can use make(). Like you said, you would need to inject the container into your objects, which is bad. Because of this, when I added the make method on the container, I also added a DI\FactoryInterface. The container implements that interface.
That interface allows to inject the container, without making your code directly coupled to the container. In short, your dependency is any factory. This would mean this configuration: