-
-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prototype scope on "factory" definitions #164
Comments
Hi! I think I see what you problem is, let me know if I'm wrong. You have something like this: return [
'Foo' => DI\factory(function () {
return new Foo();
}),
]; $foo1 = $container->get('Foo');
$foo2 = $container->get('Foo'); Here There are 2 solutions:
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 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: return [
'DI\FactoryInterface' => DI\link('DI\Container');
'Bar' => DI\object()
->constructor(DI\link('DI\FactoryInterface'));
]; So in your code you have: class Bar
{
private $foo;
public function __construct(FactoryInterface $factory)
{
$this->foo = $factory->make('Foo');
}
} And notice there is nothing in your code that couples you to the container. |
Thanks for quick answer.
Yes, everything is correct.
Also I took a look to the source code. It turned out, that adding scope to I've added this changes and it seems that everything works really well. Also, I have some troubles with running tests (got 21 fails). Here is my commit for this fix: avant1@2dfa47c |
Hi, great! You should be able to create a pull request here: avant1/PHP-DI@mnapoli:4.2...4.2 We'll see in the pull request which tests are failing and how to fix it. |
#164 Prototype scope on "factory" definitions
Hello.
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: