Repository navigation
Open
Labels
Description
Container Inconsistency: get() vs getShared()
Current behavior
Di::getShared() (Di.zep:345) writes to the shared-instance cache unconditionally:
public function getShared(string! name, parameters = null) -> var
{
let name = this->resolveAlias(name);
if (!isset($this->sharedInstances[name])) {
let this->sharedInstances[name] = this->get(name, parameters); // always stores
}
return this->sharedInstances[name];
}Di::get() (Di.zep:175) only reads/writes the cache when the service isShared():
if fetch service, this->services[name] {
let isShared = service->isShared();
if isShared && isset this->sharedInstances[name] { // guarded by isShared
return this->sharedInstances[name];
}
}
...
if service !== null {
let instance = service->resolve(parameters, this);
if isShared { // only caches when shared
let this->sharedInstances[name] = instance;
}
}Resulting asymmetry for a service registered as non-shared
After a single getShared("foo") call:
| Call | Returns |
|---|---|
getShared("foo") |
the pinned instance in sharedInstances (same each time) |
get("foo") |
a brand-new instance every time (ignores the pinned entry) |
getService("foo")->isShared() |
still false |
So getShared() silently makes a non-shared service behave as a singleton through getShared(), while get() keeps treating it as non-shared. The sharedInstances array ends up holding an entry that get() deliberately ignores.
Solution
getShared() should respect the service definition: it should not pin a non-shared service into sharedInstances. Conceptually getShared() is meant to force shared resolution for a one-off call, but pinning the instance permanently is the part that creates the divergence from get(). .
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog
Activity