Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Parameter Laravel Package

respect/parameter

View on GitHub
Deep Wiki
Context7

Respect\Parameter

Resolves function and constructor parameters from a PSR-11 container by type.

Install

composer require respect/parameter

Usage

Resolve arguments

For each parameter the resolver tries, in order:

  1. An explicit named argument (keyed by parameter name)
  2. A positional argument already matching the parameter type
  3. The container, matched by type (non-builtin)
  4. The next positional argument
  5. The parameter's default value
  6. null

A 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);

Named arguments

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']);

Bind to the interface

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)
    {
    }
}

Reflect any callable

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

Check accepted types

ContainerResolver::acceptsType($reflection, LoggerInterface::class); // true/false

API

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.

License

ISC. See LICENSE.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus