framework icon indicating copy to clipboard operation
framework copied to clipboard

Improve error message when calling Event::listen() with a string event and null listener

Open runbing opened this issue 2 months ago • 2 comments

Laravel Version

12.0

PHP Version

8.4.0

Database Driver & Version

8.0.31

Description

In Laravel 12, if you call Illuminate\Support\Facades\Event::listen(TestEvent::class); without providing a listener, the current Illuminate\Events\Dispatcher::listen() method registers a null listener, which later causes an unexpected error when the event is dispatched (attempting to call null as a function while invoking the Illuminate\Events\Dispatcher::makeListener() method).

[2025-09-22 16:28:08] local.ERROR: Value of type null is not callable {"exception":"[object] (Error(code: 0): Value of type null is not callable at /home/xxx/project/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:488)

This error is vague and can be confusing and time-consuming for developers to debug, especially since the method signature allows $listener = null. Had the framework thrown a clear and friendly exception before the error occurred, it would have immediately pointed out the problem instead of triggering a native PHP error—especially when using asynchronous queues.

Steps To Reproduce

  1. Create a new Laravel 12 project.
  2. Define a simple event class: TestEvent.
  3. Manually register the event without a listener: Event::listen(TestEvent::class);
  4. Dispatch the event: event(new TestEvent());
  5. Observe the resulting error.
Image

runbing avatar Sep 22 '25 12:09 runbing