FYI Singletons are not behaving like singletons if you resolve them from container with parameters #56672
Replies: 5 comments 1 reply
-
In Laravel, when you bind a class as a singleton, the container should always return the same instance.
if (isset($this->instances[$abstract]) && ! $needsContextualBuild) {
return $this->instances[$abstract];
} So, passing parameters sets
References: |
Beta Was this translation helpful? Give feedback.
-
It makes sense that the container generates a new instance based on parameters but I would like to see it return the same instance if you generate with the same parameters again. So, basically, unique singletons based on parameters |
Beta Was this translation helpful? Give feedback.
-
I try to document it here: laravel/docs#10741 |
Beta Was this translation helpful? Give feedback.
-
Taylor Otwell said:
What would be a clear example to illustrate this in the docs? |
Beta Was this translation helpful? Give feedback.
-
\app(Class::class); // or resolve
// vs
\app(Class::class, [
'param1' => 1,
'param2' => 2,
]); |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
https://laravel.com/docs/12.x/container#binding-a-singleton
This is a good behavior that is not documented:
Beta Was this translation helpful? Give feedback.
All reactions