Skip to content

feat(container): add singleton lifetime support - #2292

Draft
brendt wants to merge 6 commits into
3.xfrom
feat-container-lifetime
Draft

feat(container): add singleton lifetime support#2292
brendt wants to merge 6 commits into
3.xfrom
feat-container-lifetime

Conversation

@brendt

@brendt brendt commented Sep 9, 2026

Copy link
Copy Markdown
Member

No description provided.

@brendt
brendt marked this pull request as draft September 9, 2026 09:41
@brendt

brendt commented Sep 9, 2026

Copy link
Copy Markdown
Member Author

I'm not sure I'm 100% happy with this approach. It would be more clean if we registered singleton definitions in some kind of config, so that we had a list of attributes available after discovery.

That's a pretty large refactor however. The container never used this pattern because it was literally the first component ever to be written for Tempest, before we followed the best practice of discovery writing to config.

It's not the end of the world, and I think I'm fine with the current approach, but I'd love some input @innocenzi @xHeaven

@github-actions

github-actions Bot commented Sep 9, 2026

Copy link
Copy Markdown

Benchmark Results

Comparison of feat-container-lifetime against 3.x (006169824ba6ae00edd0d87662f01bc6baddc401).

Open to see the benchmark results
Benchmark Set Mem. Peak Time Variability
ViewRenderBench(benchPlainHtml) - 22.036mb +0.01% 393.457μs -7.78% ±1.83% -18.85%
ViewRenderBench(benchExpressions) - 24.593mb +0.01% 431.011μs -18.96% ±2.25% +20.63%
ViewRenderBench(benchControlFlow) - 44.503mb +0.01% 588.368μs -18.64% ±2.43% +83.15%
ViewRenderBench(benchViewComponent) - 69.743mb +0.00% 1.209ms -12.50% ±1.96% -24.97%
ContainerBench(benchRegisterSingletonInstance) - 6.107mb +2.17% 1.803μs +5.04% ±0.95% -6.05%

Generated by phpbench against commit 94917bc

@brendt
brendt force-pushed the feat-container-lifetime branch from dd86ee6 to d8f3132 Compare September 9, 2026 10:09
@brendt
brendt requested a review from xHeaven September 9, 2026 10:09
@aidan-casey

Copy link
Copy Markdown
Member

I have several thoughts here...

  1. I really dislike confusing singletons and scoped objects which are two separate things.

  2. Remember that the whole idea of discovery is that it is gluing together things that can be manually done under the hood. We have no way of setting the singleton lifecycle when registering here.

@innocenzi

innocenzi commented Sep 9, 2026

Copy link
Copy Markdown
Member

I actually think the idea is good, and does align with .NET. "Transient" is just another lifetime, exactly like .NET does.

@aidan-casey's second point is valid though, we should be able to specify the lifetime when binding services directly through the Container instance.

We could have this:

// generic
$container->bind(..., lifetime: Lifetime::REQUEST);

// shorthands
$container->transient(...);
$container->singleton(...);
$container->perRequest(...);

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.

3 participants