diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index 30860a39d1bc..57fa4af2fa79 100755 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -316,6 +316,8 @@ protected function parseEventAndPayload($event, $payload) { if (is_object($event)) { [$payload, $event] = [[$event], get_class($event)]; + } else if (class_exists($event)) { + [$payload, $event] = [[new $event(...$payload)], $event]; } return [$event, Arr::wrap($payload)]; diff --git a/tests/Events/EventsDispatcherTest.php b/tests/Events/EventsDispatcherTest.php index 696fc32761ff..e209fa8a8c35 100755 --- a/tests/Events/EventsDispatcherTest.php +++ b/tests/Events/EventsDispatcherTest.php @@ -583,6 +583,21 @@ public function testInvokeIsCalled() unset($_SERVER['__event.test']); } + + public function testDispatchWithObjectAndClassWithArguments() + { + unset($_SERVER['__event.test']); + $d = new Dispatcher; + $d->listen(TestEvent1::class, [TestListener4::class, 'handle']); + + $test = 'hello'; + + $d->dispatch(new TestEvent1($test)); + $this->assertSame($test, $_SERVER['__event.test']); + + $d->dispatch(TestEvent1::class, [$test]); + $this->assertSame($test, $_SERVER['__event.test']); + } } class TestListenerLean @@ -726,3 +741,18 @@ public function handle() $_SERVER['__event.test'][] = 'handle-3'; } } + +class TestEvent1 +{ + public function __construct( + public string $test + ) {} +} + +class TestListener4 +{ + public function handle(TestEvent1 $exampleEvent) + { + $_SERVER['__event.test'] = $exampleEvent->test; + } +}