This version is outdated. You should upgrade your project to Mako 9.1 or Mako 10.0!
Learn more

Events



Mako includes two ways of handling events, an event listener and a trait that makes your class observable.


Event listener

The register method lets you register an event handler that will get executed when the event is triggered. You can register multiple handlers for the same event. They will be executed in the order that they were registered.

$this->event->register('foobar', function()
{
	return 'foobar event handler';
});

You can also handle your events using a class instead of a closure.

$this->event->register('foobar', FoobarHandler::class);

Class handlers will be instantiated by the dependency injection container so you can easily inject your dependencies through the constructor. Both closure and class handlers will be executed by the Container::call() and all dependencies will automatically be injected there as well.

<?php

namespace app\events;

use mako\event\EventHandlerInterface;

class FoobarHandler implements EventHandlerInterface
{
	public function handle()
	{
		return 'foobar event handler';
	}
}

The has method will return TRUE if an event handler has been registered and FALSE if not.

$registered = $this->event->has('foobar');

The clear method will clear all event handlers registered for an event.

$this->event->clear('foobar');

The override method will clear all previously registered handlers for an event before registering a new handler.

$this->event->override('foobar', function()
{
	return 'foobar event 1';
});

The trigger method run all handlers for the registered event and return an array containing all the return values.

$values = $this->event->trigger('foobar');

You can also pass arguments your handlers using the optional second parameter.

$values = $this->event->trigger('foobar', [1, 2, 3]);

The third optional parameter lets you stop event handling if one of the handlers return FALSE.

$values = $this->event->trigger('foobar', [1, 2, 3], true);