Summary
Introduce instance binding support to the Di container via a new Di::set(string $abstract, object $instance) method. This allows prebuilt object instances to be registered directly in the container and resolved through Di::get() without reflection-based instantiation.
The change is fully compatible with PHP 7.4 and fits the current static container architecture without redesigning the dependency resolution flow.
Motivation
Current DI supports only class-to-class registration and reflective instantiation. There is no clean way to:
- bind prebuilt configuration objects
- inject environment-specific services
- register test doubles without mocking
- override services with ready instances
Instance binding adds this capability with minimal architectural impact.
Proposed API
public static function set(string $abstract, object $instance): void
Test Coverage (Add)
- Can bind instance to interface and resolve via get()
- Throws when instance does not match abstract
- Throws when container entry already exists
- Works when abstract was not previously registered
Summary
Introduce instance binding support to the
Dicontainer via a newDi::set(string $abstract, object $instance)method. This allows prebuilt object instances to be registered directly in the container and resolved throughDi::get()without reflection-based instantiation.The change is fully compatible with PHP 7.4 and fits the current static container architecture without redesigning the dependency resolution flow.
Motivation
Current DI supports only class-to-class registration and reflective instantiation. There is no clean way to:
Instance binding adds this capability with minimal architectural impact.
Proposed API
Test Coverage (Add)