Tuesday, February 28, 2017

Understanding PHP event dispatching systems

I've never done anything event-driven in PHP, only Node.js, so I'm trying to understand how event dispatching systems work in PHP (such as Laravel events, CakePHP events and Symfony event dispatcher).

This example is in the Laravel docs:

protected $listen = [
    'App\Events\OrderShipped' => [
        'App\Listeners\SendShipmentNotification',
    ],
];

Since the lifetime of a PHP script running on Apache is basically the lifetime of the request, does this mean all the event listeners are instantiated with each request?

So if I have 120 listeners in my application (i.e. listed in this $listen property), are all 120 of them going to be instantiated every time a page is visited? Or do the listener objects only get instantiated when the appropriate events are dispatched?

It seems quite inefficient for the listeners to be instantiated with each request when in the entire duration of the request there might not even be a single event fired.

Is this something that I should even be concerned about?




via rink.attendant.6

Advertisement