You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Illuminate\Events\Dispatcher has method listen with two arguments $events and $listener:
/** * Register an event listener with the dispatcher. * * @param string|array $events * @param \Closure|string $listener * * @return void */publicfunctionlisten($events, $listener)
{
// ...
}
Next, this method using another method makeListener.
The makeListener() method is ready to use argument $listener as an array:
/** * Register an event listener with the dispatcher. * * @param \Closure|string $listener * @param bool $wildcard * * @return \Closure */publicfunctionmakeListener($listener, $wildcard = false)
{
if (is_string($listener)) {
return$this->createClassListener($listener, $wildcard);
}
// ===================== see next line ====================if (is_array($listener) && isset($listener[0]) && is_string($listener[0])) {
return$this->createClassListener($listener, $wildcard);
}
// ...
}
If we make event subscriber class like this:
<?phpnamespaceApp\Listeners;
useApp\Events\UserCreatedEvent;
useIlluminate\Events\Dispatcher;
useIlluminate\Support\Facades\Log;
class UserEventSubscriber
{
publicfunctionhandle(UserCreatedEvent$event)
{
Log::info($event->getTarget()->username);
}
publicfunctionsubscribe(Dispatcher$dispatcher)
{
$dispatcher->listen(UserCreatedEvent::class, [$this, 'handle']); // using array as closure
}
}
event listener works fine, but IDE shows Expected parameter of type '\Closure|string', 'array' provided in method listen.
I think it would be good to change the type hint for the $listener parameter from @param \Closure|string $listener to @param \Closure|array|string $listener.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Illuminate\Events\Dispatcher
has method listen with two arguments$events
and$listener
:Next, this method using another method makeListener.
The
makeListener()
method is ready to use argument$listener
as an array:If we make event subscriber class like this:
event listener works fine, but IDE shows
Expected parameter of type '\Closure|string', 'array' provided
in methodlisten
.I think it would be good to change the type hint for the
$listener
parameter from@param \Closure|string $listener
to@param \Closure|array|string $listener
.Beta Was this translation helpful? Give feedback.
All reactions