We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Event listeners order

Hello everybody. Let's try to execute the following code:

$em = new \Phalcon\Events\Manager();
$em->attach('component:test', function ($e) {echo '1 ';});
$em->attach('component:test', function ($e) {echo '2 ';});
$em->attach('component', function ($e) {echo '3 ';});
$em->attach('component', function ($e) {echo '4 ';});
$em->fire('component:test', $this);

The expected result - 1 2 3 4.
But in fact it's - 3 4 1 2

It seems like component listeners executed before concrete event listeners.
That behaviour is not documented and the order of listeners becomes unclear with such logic, as for me.
Thanks!



34.6k
Accepted
answer

Thanks for the answer, it may help me if I suggest to make a pull-request :) Nevertheless I think that behaviour should be documented, or changed to make the order of listeners more predictable

Events attached directly to an event name are executed after the components that aren't attached directly.