Skip to content

Fix: Allow lazy ghosts for services without constructor arguments#329

Open
mildabre wants to merge 16 commits intonette:masterfrom
mildabre:fix/lazy-without-constructor-args
Open

Fix: Allow lazy ghosts for services without constructor arguments#329
mildabre wants to merge 16 commits intonette:masterfrom
mildabre:fix/lazy-without-constructor-args

Conversation

@mildabre
Copy link
Copy Markdown

Fix: Allow lazy ghosts for services without constructor arguments

Problem

Currently, ServiceDefinition::canBeLazy() prevents lazy ghost creation for services without constructor arguments:

&& ($this->creator->arguments || $this->setup)

This check causes the lazy flag to be silently ignored for such services, which is incorrect behavior.

Use Case

Services with heavy initialization that doesn't require dependencies:

class ConfigParser {
    public function __construct() {
        // Heavy work without dependencies
        $this->parseYamlFile();  // 200ms
        $this->validateSchema(); // 150ms
    }
}

// Currently: lazy flag ignored, always eager initialization
$def->setLazy(true);  // Has no effect! ❌

// After fix: lazy flag respected
$def->setLazy(true);  // Creates lazy ghost ✅

Solution

Remove the constructor arguments/setup check as it's no longer necessary with PHP 8.4's native ReflectionClass::newLazyGhost() support.

The remaining checks are still valid:

  • Entity must be a string (class name)
  • Class must not be internal (PHP limitation)

Backward Compatibility

NOT a BC break - only fixes previously broken functionality
✅ All existing lazy services continue to work
✅ Enables new valid use cases

Suggested Release

Patch version (v3.3.x) as this is a bug fix, not a feature.

dg and others added 16 commits January 9, 2026 00:41
@return, ... and %parameters% are no longer supported
This is a bug fix addressing incorrect behavior where the lazy flag
was silently ignored for services without constructor arguments.

With PHP 8.4's native lazy ghost support, there is no technical
limitation preventing lazy loading for such services. This fix
enables valid use cases like services with heavy initialization
that doesn't require constructor dependencies.

This is NOT a BC break - it only fixes previously broken functionality
where explicitly set lazy=true was incorrectly ignored.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants