Boot-time Frozen Services via #[Immutable]#203
Merged
techmahedy merged 12 commits intodoppar:3.xfrom Mar 1, 2026
Merged
Conversation
added 12 commits
March 1, 2026 15:31
via #[Immutable]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Introduces a container-enforced service lifecycle boundary. Services marked
#[Immutable]are fully configurable during the boot phase, then permanently frozen for the entire request lifecycle — at the object level, not the container level.Motivation
The Problem With Mutable Singletons
In every major PHP framework today, singleton services are registered once but remain permanently mutable. Nothing prevents a controller, middleware from silently corrupting shared service state mid-request.
Example:
This is not hypothetical. It is a class of bug that is extremely hard to trace because the mutation and the symptom are separated by request boundaries, middleware layers, and injection chains. The only defence today is code review and convention — neither is enforced at runtime.
What This PR Does
A Clear Lifecycle Boundary
This PR introduces a two-phase service lifecycle enforced at the object level by the container:
What Developers Write
The entire public-facing API is a single attribute and a trait. Nothing else is required from the developer.
Mutable during boot only
Mutations blocked
How This Differs From Existing Solutions
The critical distinction from readonly class
PHP's readonly freezes on construction. You cannot do this:
The exception message
PHP offers no runtime method injection, no monkey patching, and no transparent proxy mechanism without extensions (e.g. ocramius/proxy-manager generates separate class files at build time). The freeze() + unset() approach works with PHP's semantics rather than against them.