Skip to content

Add Di::set() instance binding support to DI container #413

Description

@armanist

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)

  1. Can bind instance to interface and resolve via get()
  2. Throws when instance does not match abstract
  3. Throws when container entry already exists
  4. Works when abstract was not previously registered

Metadata

Metadata

Assignees

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions