Dynamic Invoker Utility#
The Invoker class provides a convenience wrapper around the DI container to
simplify dynamic execution and object resolution.
Overview#
use Infocyph\InterMix\DI\Container;
use Infocyph\InterMix\DI\Invoker;
$invoker = Invoker::with(new Container());
Key Features#
Feature |
Description |
|---|---|
|
Dynamically call closures, classes, methods |
|
Build object plus optionally call a method |
|
Retrieve from container by key |
|
Serialize closures and values |
|
Restore serialized closures or data |
Usage Examples#
1. Call a closure
$result = $invoker->invoke(fn () => 'hello');
2. Call a class method
$result = $invoker->invoke([MyService::class, 'boot']);
3. Serialize and restore a closure
$packed = $invoker->serialize(fn () => 42);
$fn = $invoker->unserialize($packed);
echo $fn(); // 42
4. Shared global instance
$invoker = Invoker::shared();
$data = $invoker->resolve('service');
Internals#
The invoker uses:
routeCallable()— detects callable types: closures, invokable classes, strings, or serialized closuresviaClosure()— injects closures into the container for contextual executionIntegration with
ValueSerializerfor full closure support
Next up → Attribute Injection