Skip to content

[12.x] Add support for named arguments in event dispatching and broadcasting#58913

Merged
taylorotwell merged 1 commit into
laravel:12.xfrom
ph7jack:12.x-named-arguments-on-event-dispatch
Feb 19, 2026
Merged

[12.x] Add support for named arguments in event dispatching and broadcasting#58913
taylorotwell merged 1 commit into
laravel:12.xfrom
ph7jack:12.x-named-arguments-on-event-dispatch

Conversation

@ph7jack

@ph7jack ph7jack commented Feb 19, 2026

Copy link
Copy Markdown
Contributor

Summary:
The Illuminate\Foundation\Events\Dispatchable trait currently uses func_get_args() in dispatch() and broadcast().
That pattern does not support named arguments in PHP.
At the same time, other dispatchable entry points in the framework already support named arguments (especially in jobs). This PR aligns event dispatching with that existing developer experience.

Before:

new UserSubscribed($user, $plan, $metadata);

or

event(new UserSubscribed(
    user: $user,
    plan: $plan,
    metadata: [...]
));

After:

UserSubscribed::dispatch(
    user: $user,
    plan: $plan,
    metadata: [...]
);

Benefits

  • Consistent API ergonomics across events and jobs/other dispatchable methods.
  • Better readability at call sites for events with multiple constructor parameters.
  • Safer refactors and fewer bugs caused by parameter ordering mistakes.
  • No breaking change for existing positional-argument usage.

Core framework changes:

  • Updated the dispatch and broadcast static methods in the Dispatchable trait to accept variadic arguments, enabling support for named arguments when instantiating event classes. [1] [2]

Testing improvements:

  • Added new tests in EventsDispatcherTest.php and BroadcastedEventsTest.php to verify that events can be dispatched and broadcasted using named arguments, ensuring correct instantiation and argument mapping. [1] [2]
  • Introduced DispatchableNamedArgumentsEvent and BroadcastableNamedArgumentsEvent test classes to validate named argument support in event dispatching and broadcasting. [1] [2]
  • Included necessary imports for PendingBroadcast in the test suite.

@ph7jack ph7jack changed the title Add support for named arguments in event dispatching and broadcasting [12.x] Add support for named arguments in event dispatching and broadcasting Feb 19, 2026
@shaedrich

Copy link
Copy Markdown
Contributor

Warning

Named Arguments

Named arguments are not covered by Laravel's backwards compatibility guidelines. We may choose to rename function arguments when necessary in order to improve the Laravel codebase. Therefore, using named arguments when calling Laravel methods should be done cautiously and with the understanding that the parameter names may change in the future.
https://laravel.com/docs/12.x/releases#named-arguments

@taylorotwell taylorotwell merged commit a35245a into laravel:12.x Feb 19, 2026
72 checks passed
@AndrewMast

AndrewMast commented Feb 19, 2026

Copy link
Copy Markdown
Contributor

@shaedrich Isn't that warning about named arguments in Laravel's own code? This feature allows you to use named arguments when dispatching your own events (thus your own named arguments, not Laravel's).

@shaedrich

Copy link
Copy Markdown
Contributor

Oh, okay—sorry, nevermind 😬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants