VOOZH about

URL: https://deepwiki.com/hypervel/container/4-advanced-container-features

⇱ Advanced Container Features | hypervel/container | DeepWiki


Loading...
Menu

Advanced Container Features

This document covers sophisticated container capabilities that extend beyond basic service binding and resolution. These features include automatic method invocation with dependency injection, lifecycle hooks for customizing resolution behavior, service decoration through extenders, type aliasing, and custom method bindings.

For basic container operations like bind(), make(), and get(), see Container System. For information about how the container loads configuration at bootstrap, see Configuration & Initialization.

Overview

The Container class provides several advanced features that enable complex dependency injection patterns, runtime service modification, and flexible callable invocation. These capabilities are built on top of the core Hyperf container while maintaining PSR-11 compliance.

Advanced Features Architecture


Sources: src/Container.php17-737 src/BoundMethod.php15-222

Feature Categories

The advanced features fall into three primary categories:

Feature CategoryPrimary Classes/MethodsPurpose
Method InvocationBoundMethod, Container::call()Automatically resolve and inject dependencies when invoking callables
Lifecycle CallbacksbeforeResolving(), resolving(), afterResolving()Hook into the resolution pipeline to modify behavior
Extensibilityextend(), alias(), bindMethod()Decorate instances, create name aliases, override method resolution

Sources: src/Container.php17-737 src/BoundMethod.php15-222

Method Invocation with Dependency Injection

The BoundMethod class provides automatic dependency injection for arbitrary callables. When you call Container::call(), it delegates to BoundMethod::call() which resolves all callable parameters automatically.

Callable Resolution Flow


Sources: src/BoundMethod.php25-74 src/Container.php437-440 src/Container.php264-295

Parameter Resolution Strategy

When BoundMethod resolves parameters for a callable, it follows a specific priority order:

PrioritySourceDescription
1Explicit parameters (by name)Parameters passed to call() matching parameter name
2Explicit parameters (by position)Parameters passed to call() matching parameter position
3Explicit parameters (by type)Parameters passed to call() matching the type hint
4Default valuesParameter has a default value defined
5Container resolutionContainer can resolve the type hint
6NullableParameter allows null value
7ExceptionThrows BindingResolutionException

The resolution logic is implemented in getDependencyParameters() at src/BoundMethod.php76-115

For detailed information about method invocation, see Method Invocation & Dependency Injection.

Sources: src/BoundMethod.php76-115

Lifecycle Callbacks

The container provides three callback hooks that fire during service resolution: before resolving, during resolving, and after resolving. Each hook supports both global callbacks (fired for all resolutions) and type-specific callbacks (fired only for specific types).

Resolution Pipeline with Callbacks


Sources: src/Container.php109-132 src/Container.php467-514 src/Container.php522-610

Callback Storage Structure

The container maintains six separate arrays for lifecycle callbacks:


Sources: src/Container.php59-94

For detailed information about registering and using lifecycle callbacks, see Lifecycle Callbacks.

Extenders, Aliases, and Method Bindings

Beyond lifecycle callbacks, the container provides three additional extensibility mechanisms:

Extenders

Extenders allow you to modify or decorate resolved instances. Unlike lifecycle callbacks which observe resolution, extenders transform the instance.

Aliases

Aliases create alternative names for service bindings, enabling flexible type references.

Method Bindings

Method bindings allow you to override how specific methods are invoked when called through Container::call().


Sources: src/Container.php264-295 src/BoundMethod.php152-168

For comprehensive coverage of these features, see Extenders, Aliases & Method Bindings.

Feature Interaction Matrix

The following table shows how advanced features interact with each other:

FeatureRespects AliasesFires CallbacksUses ExtendersCan Override
make()Via callbacks/extenders
call()Via method bindings
extend()Modifies existing extenders
alias()N/A
bindMethod()Completely overrides method

Sources: src/Container.php109-132 src/Container.php437-440 src/Container.php314-324 src/Container.php374-383 src/Container.php272-275

Rebinding and Instance Refresh

When a service binding is updated, the container provides mechanisms to notify dependent code and refresh instances:

Rebound Callbacks

Rebound callbacks fire whenever a binding is replaced:

Instance Refresh

The refresh() method provides a convenient way to update a property or call a setter when a service is rebound:


This is implemented at src/Container.php402-407 and internally uses rebinding().

Sources: src/Container.php388-419 src/Container.php243-259 src/Container.php329-349 src/Container.php314-324

Advanced Usage Patterns

Conditional Method Execution

Method bindings enable conditional or contextual method execution:


Implementation at src/Container.php272-287

Service Decoration

Extenders enable the decorator pattern without modifying service definitions:


Implementation at src/Container.php314-324

Type-Specific Initialization

Lifecycle callbacks allow type-specific initialization logic:


Implementation at src/Container.php485-496 src/Container.php552-562

Sources: src/Container.php272-287 src/Container.php314-324 src/Container.php485-496 src/Container.php552-562

Summary

The advanced container features provide powerful mechanisms for:

  1. Automatic dependency injection - Invoke any callable with automatic parameter resolution
  2. Resolution lifecycle control - Hook into before, during, and after resolution phases
  3. Runtime service modification - Decorate, extend, or replace resolved instances
  4. Flexible naming - Create aliases for type flexibility
  5. Custom invocation logic - Override specific method calls with custom bindings

These features are explored in detail in the following subsections:

Sources: src/Container.php17-737 src/BoundMethod.php15-222