
All modern applications need solid and flexible event components. Zend Framework provides one such component, zend-eventmanager. The zend-eventmanager helps to design high level architecture and supports subject/observer pattern and aspect oriented programming.
The event manager can be installed using the Composer as specified below −
composer require zendframework/zend-eventmanager
The core concepts of the event manager are as follows −
Event − Event is arbitrarily named action, say greet.
Listener − Any PHP callback. They are attached to the events and gets called when the event is triggered. The default signature of Listener is −
function(EventInterface $e)
EventInterface Class − Used to specify the event itself. It has methods to set and get event information like name (set/getName), target (get/setTarget) and parameter (get/setParams).
EventManager class − The instance of the EventManager tracks all the defined events in an application and its corresponding listeners. The EventManager provides a method, attach to attach listener to an event and it provides a method, trigger to trigger any pre-defined event. Once trigger is called, EventManager calls the listener attached to it.
EventManagerAwareInterface − For a class to support event based programming, it needs to implement the EventManagerAwareInterface. It provides two methods, setEventManager and getEventManager to get and set the event manager.
Let us write a simple PHP console application to understand the event manager concept. Follow the steps given below.
Create a folder “eventapp”.
Install zend-eventmanager using the composer.
Create a PHP file Greeter.php inside the “eventapp” folder.
Create class Greeter and implement the EventManagerAwareInterface.
require __DIR__ . '/vendor/autoload.php';
class Greeter implements EventManagerAwareInterface {
// code
}
Here, require is used to autoload all composer installed components.
Write the setEventManager method in class Greeter as shown below −
public function setEventManager(EventManagerInterface $events) {
$events->setIdentifiers([ __CLASS__, get_called_class(),]);
$this->events = $events;
return $this;
}
This method sets the current class into the given event manager ($events argument) and then sets the event manager in local variable $events.
The next step is to write the getEventManager method in class Greeter as shown below −
public function getEventManager() {
if (null === $this->events) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
The method gets the event manager from a local variable. if it is not available, then it creates an instance of event manager and returns it.
Write a method, greet, in class Greeter.
public function greet($message) {
printf("\"%s\" from class\n", $message);
$this->getEventManager()->trigger(__FUNCTION__, $this, $message ]);
}
This method gets the event manager and fires / triggers events attached to it.
The next step is to create an instance of the Greeter class and attach a listener to its method, greet.
$greeter = new Greeter();
$greeter->getEventManager()->attach('greet', function($e) {
$event_name = $e->getName();
$target_name = get_class($e->getTarget());
$params_json = json_encode($e->getParams());
printf("\"%s\" event of class \"%s\" is called." .
" The parameter supplied is %s\n",
$event_name,
$target_name,
$params_json);
});
The listener callback just prints the name of the event, target and the supplied parameters.
The complete listing of the Greeter.php is as follows −
<?php
require __DIR__ . '/vendor/autoload.php';
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
class Greeter implements EventManagerAwareInterface {
protected $events;
public function setEventManager(EventManagerInterface $events) {
$events->setIdentifiers([__CLASS__, get_called_class(), ]);
$this->events = $events;
return $this;
}
public function getEventManager() {
if (null === $this->events) {
$this->setEventManager(new EventManager());
}
return $this->events;
}
public function greet($message) {
printf("\"%s\" from class\n", $message);
$this->getEventManager()->trigger(__FUNCTION__, $this, [$message ]);
}
}
$greeter = new Greeter();
$greeter->greet("Hello");
$greeter->getEventManager()->attach('greet', function($e) {
$event_name = $e->getName();
$target_name = get_class($e->getTarget());
$params_json = json_encode($e->getParams());
printf("\"%s\" event of class \"%s\" is called." . " The parameter supplied is %s\n",
$event_name,
$target_name,
$params_json);
});
$greeter->greet("Hello");
Now, run the application in the command prompt php Greeter.php and the result will be as follows −
"Hello" from class "Hello" from class "greet" event of class "Greeter" is called. The parameter supplied is ["Hello"]
The above sample application explains only the basics of an event manager. The Event manager provides many more advanced options such as Listener Priority, Custom Callback Prototype / Signature, Short Circuiting, etc. The Event manager is used extensively in the Zend MVC framework.