-
Notifications
You must be signed in to change notification settings - Fork 5
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
Reuse instance? #7
Comments
Are you using |
Sorry, accidentally closed. I meant what are you using to get your objects. |
Yes, I'm using Is there any methods to get my objects? |
And what if I want to get the same instance in Automatic Dependency Resolution? |
Hello, But you can do like this: $container = new League\Di\Container;
$container->bind('\League\Di\Stub\Bar');
$container->extend('\League\Di\Stub\Bar', function ($container, $instance) {
static $singleton;
if (is_null($singleton)) {
$singleton = $instance;
}
return $singleton;
});
// This will return the same Bar instance on each call
$container->resolve('\League\Di\Stub\Bar'); I there was in if ($rawObject instanceof \Closure || $rawObject instanceof Definition) {
return $rawObject();
}
return $rawObject; You could also do $container->bind('\League\Di\Stub\Bar', $instanceOfBarAlreadyExisting);
// This will return the $instanceOfBarAlreadyExisting instance on each call
$container->resolve('\League\Di\Stub\Bar'); Would you like a PR with proper unit tests? |
@adriengibrat Thank you for your explanation. But I still want a short way, so I sent PR as a trial. #8 |
Each time we get an object, di returns a new instance of it.
How about adding a way to get the same instance to be returned for all calls?
The text was updated successfully, but these errors were encountered: