Resolves function and constructor parameters from a PSR-11 container by type.
composer require respect/parameter
For each parameter the resolver tries, in order:
nullA trailing variadic parameter receives a matching named argument (if any) followed by every remaining positional argument.
use Respect\Parameter\ContainerResolver;
function notify(Mailer $mailer, Logger $logger, string $to, string $subject = 'Hi') {
// ...
}
$resolver = new ContainerResolver($container);
$args = $resolver->resolve(new ReflectionFunction('notify'), ['[email protected]']);
// [Mailer, Logger, '[email protected]', 'Hi'] — ordered, ready to splat
The result is an ordered list, so spread it straight into the call or constructor:
notify(...$args);
// or
$reflection->newInstanceArgs($args);
resolve() accepts named arguments too — keyed by parameter name, taking precedence over the
container; the remaining parameters are filled by type and defaults:
$args = $resolver->resolve($constructor, ['username' => 'admin']);
Type-hint Resolver (the resolve() contract) rather than the concrete ContainerResolver to stay
decoupled from the implementation:
use Respect\Parameter\Resolver;
final class Factory
{
public function __construct(private Resolver $resolver)
{
}
}
Convert any callable form into a ReflectionFunctionAbstract:
use Respect\Parameter\ContainerResolver;
ContainerResolver::reflectCallable(fn() => ...); // Closure
ContainerResolver::reflectCallable([$obj, 'method']); // Array callable
ContainerResolver::reflectCallable(new Invocable()); // __invoke object
ContainerResolver::reflectCallable('strlen'); // Function name
ContainerResolver::reflectCallable('DateTime::createFromFormat'); // Static method
ContainerResolver::acceptsType($reflection, LoggerInterface::class); // true/false
| Method | Type | Description |
|---|---|---|
resolve($reflection, $arguments) |
instance | Resolve named/positional arguments + container into an ordered list<mixed>, expanding variadics |
reflectCallable($callable) |
static | Any callable to ReflectionFunctionAbstract |
acceptsType($reflection, $type) |
static | Check if any parameter accepts a type |
ContainerResolver implements Resolver.
ISC. See LICENSE.
How can I help you explore Laravel packages today?