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.
install event manager
the event manager can be installed using the composer as specified below −
composer require zendframework/zend-eventmanager
concepts of the event manager
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.
example
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.