Skip to content

[BUG]: Di get/getShared inconsistency #17074

Open

Description

@niden

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(). .

Activity

  1. self-assigned this
    on Jun 5, 2026
  2. added
    bug
    on Jun 5, 2026
  3. added theissue type on Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

7.0bugstatus: medium

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions