jump to navigation

Using ZF2’s Event Manager Component as Event Driven Programming Example March 27, 2014

Posted by Tournas Dimitrios in Zend Framework 2.
Tags: ,
1 comment so far

Zend Framework 2 logoA fundamental principle of Java , .NET platform and other  popular programming languages is based on Event-driven Architecture (EDA). Simply put, the idea is that activities occur in response to events . In other words, activities are attached to events, and when a given event occurs, its attached activity[ies] is[are] executed.This programming paradigm is named  “event-driven programming”  and is nowadays adopted by almost all programming languages (client / server side) . Although event-driven programming was adopted by each programming language for a completely different purpose, the ultimate goal was to write applications that are flexible, scalable and “plug-able” at run-time . Of course , each language has its own implementation, but at the end , all of them were envisioned by the same concepts .

Event-driven programs can be written in any language, although the task is easier in languages that provide high-level abstractions, such as Closures .Back in December 2010 , PHP 5.3 was released . Three of the most notable new features of that release were : support for namespacesLate static binding and Lambda Functions / Closures . At that time , many PHP Frameworks started to flirt with event-driven architecture which leaded to a permanent relation . Laravel-4 , Symfony-2 and Zend Framework-2 have now event driven functionality incorporated into their core . But that’s just history, lets bypass this introductory chatter and go straight to the point .

Prerequisites : The reader should have a good grasp of OOP concepts . Also knowing to use Composer’s basic functionality is helpful , as it will be used to offload basic tasks like : downloading the required component and using its auto-load functionality .
Composer file used for this article :

{
	"repositories": [
		{
		    "type": "composer",
		    "url": "https://packages.zendframework.com/"
		}
	],
	"require": {
"zendframework/zend-eventmanager": "2.*",

	}

}

 

This article will demonstrate a practical example of using “events” into a standalone code base . For no specific reason , Zend Framework 2’s “Event Manager” component was used to off-load the implementation of an “Observer Design pattern” (we don’t forget the DRY principle) .We could had used Laravel’s , Symfony’s or similar components provided from other component-based PHP Frameworks .

 

Terminology first : The way how events are raised and how listeners are attached on the events is a part of a core in many modern Frameworks. It plays an important role in some enterprise design patterns (MVC, for example) . The basic event terminology includes the following terms:

  • EventManager — is just an object that holds collection of listeners for one or more named events and which trigger events.
  • Event — is a named action .
  • Listener — is a php callback (Closure) that can react to an event
  • Target — is an object that creates events

hooray picturehooray — hooray  , boring introduction is ended . Let’s have fun ……..
A two thousand meter view : Three necessary steps to start using events are :

  • Step1: An EventManager Object (or a derivative) . Our example below will use an abstract “EventProvider” Class which extends ZF’s EventManager .
  • Step2: Registering (attaching) one or more listeners on one or more events .
  • Step3: Calling an event (triggering an event) . All attached listeners will take actions . For illustration and simplicity our examples below use closures as listeners . However, any valid PHP callback can be attached as a listeners : PHP function names, static class methods, object instance methods, functions, or closures.

The following example was copied from ZF’s documentation :

use Zend\EventManager\EventManager;

$events = new EventManager();
$events->attach('do', function ($e) {
    $event = $e->getName();
    $params = $e->getParams();
    printf(
        'Handled event "%s", with parameters %s',
        $event,
        json_encode($params)
    );
});

$params = array('foo' => 'bar', 'baz' => 'bat');
$events->trigger('do', null, $params);

(more…)

Advertisements