<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Views From The Hill &#187; Event</title>
	<atom:link href="http://dustint.com/archives/tag/event/feed" rel="self" type="application/rss+xml" />
	<link>http://dustint.com</link>
	<description>Tales Of A (Former) SFU Computing Scientist</description>
	<lastBuildDate>Thu, 17 Jun 2010 20:14:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Building a PHP Publish / Subscribe System</title>
		<link>http://dustint.com/archives/38</link>
		<comments>http://dustint.com/archives/38#comments</comments>
		<pubDate>Tue, 28 Jul 2009 07:14:21 +0000</pubDate>
		<dc:creator>Dustin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[How-To]]></category>
		<category><![CDATA[Event]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[publish subscribe]]></category>

		<guid isPermaLink="false">http://dustint.com/?p=38</guid>
		<description><![CDATA[First post in a while. I have been busy working on some large application and finishing up my Computing Science Degree. Now that the piece of paper is safely secured above my fireplace, I&#8217;ve been trying to find time to do a new post. I have been building a large application and one of the [...]]]></description>
			<content:encoded><![CDATA[<p>First post in a while. I have been busy working on some large application and finishing up my Computing Science Degree. Now that the piece of paper is safely secured above my fireplace, I&#8217;ve been trying to find time to do a new post.</p>
<p>I have been building a large application and one of the requirements is to trigger a certain action when an event happens. For example, trigger the sending of an alert email when a database row is updated. Some programmers may be tempted to simply hard-code this functionality into the model class, however this doesn&#8217;t give very strict class encapsulation, and can quickly become un-maintainable.</p>
<p>I use the Dojo Javscript toolkit for most of my dynamic front-end functionality. It has implemented a <a href="http://en.wikipedia.org/wiki/Publish/subscribe">publish/subscribe system</a> using  <a href="http://docs.dojocampus.org/dojo/publish"><code>dojo.publish()</code></a> and <a href="http://docs.dojocampus.org/dojo/subscribe"><code>dojo.subscribe()</code></a>. This article will describe the implementation of a similar system using PHP.<span id="more-38"></span></p>
<h1>Overview</h1>
<p>The concept I wanted to implement consists of three parts:</p>
<ol>
<li><strong>The Listener</strong><br />
This is a class that is interested in the events that are thrown by some other class. Keeping with the above example, tihs class may implement the functionality of sending the alert emails when the database row is updated</li>
<li><strong>The Event</strong><br />
To give the listener some data to act on, an event (message) is passed from the triggering object to the listener. This event may contain information such as the column values for the updated row.</li>
<li><strong>The Dispatcher</strong><br />
The dispatcher is the glue that holds everything together. When an object wants to trigger and event it passes it to the dispatcher <em>(publishes it)</em>. When a listener class wants to listen for an event, it will ask the dispatcher to inform it when the event of a specified type is passed (<em>subscribe</em>).</li>
</ol>
<p>Some other food for thought:</p>
<ul>
<li>A listener may only want to subscribe to an event that is published by a certain resource, so there must be a way to track who published the event</li>
<li>A listener may want to subscribe to all events that are published by a resource or to a specific event independent of the resource, so wildcards should be permitted</li>
</ul>
<h1>Implementation</h1>
<h2>The Event Object</h2>
<p>The event object, in its simplicity, is a very basic class. As mentioned above, it needs to track a minimum of:</p>
<ol>
<li>The resource that published the event</li>
<li>The name (/type) of the event</li>
<li>Some (optional) data to be passed to the listener</li>
</ol>
<p>A very simple example of an event object may be the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Event <span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * The name of the resource publishing this event
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$resourceName</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * The name of this event
	 * @var string
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$eventName</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Any data associated with this event
	 * @var mixed
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * @param string $resourceName	name of the publisher
	 * @param string $eventName		name of the event
	 * @param mixed $data			[OPTIONAL] Additional event data
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resourceName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$eventName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resourceName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$resourceName</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">eventName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$eventName</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Depending on your goals, you may want to make $resourceName, $eventName, and $data private attributes and use getters / setters. Additionally, if you do not wish to pass the eventName and, rather, use object inheritance you could implement the event object in either of the following ways:</p>
<p>Eg 1: Manually set the event name.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> UpdateEvent <span style="color: #000000; font-weight: bold;">extends</span> Event
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$eventName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Update'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resourceName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resourceName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$resourceName</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Eg 2: Automatically parse the event name from the class name</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> UpdateEvent <span style="color: #000000; font-weight: bold;">extends</span> Event
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resourceName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		parent<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$resourceName</span><span style="color: #339933;">,</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>The Listener Object</h2>
<p>The Listener object is also has a very simple job. It simply accepts an event object and does something with it. The only catch is that all listener objects should have the same method which accepts the event. Because of this, it makes sense to use an interface.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">interface</span> ListenerInterface
<span style="color: #009900;">&#123;</span>
	<span style="color: #009933; font-style: italic;">/**
	 * Accepts an event and does something with it
	 *
	 * @param Event $event	The event to process
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> publish<span style="color: #009900;">&#40;</span>Event <span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I am going to implement a very simple listener class for the purpose of this demo. This listener outputs a string saying what event was fired, and for which resource. EG:  published a .</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> EchoListener implements ListenerInterface
<span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> publish<span style="color: #009900;">&#40;</span>Event <span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$event-&gt;resourceName}</span> published a <span style="color: #006699; font-weight: bold;">{$event-&gt;eventName}</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>The Dispatch Object</h2>
<p>The dispatch object is by far the most complex of all the components. It handles both the subscription of listeners and the dispatching of events. For my actual implementation I needed more than one backend for my dispatch object (Both a hard-coded memory based one, and a user-defined backend based on mysql). For simplicities sake I am going to demonstrate only the memory-based backend here, and leave the implementation of multiple-backends as an exercise to the user.</p>
<p>Without further adeau, here is the class in its entirety. I will discuss each part individually below:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Dispatcher <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Associative array of listeners.
     * Indicies are: [resourceName][event][listener hash]
     *
     * @var array
     */</span>
    protected <span style="color: #000088;">$_listeners</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Subscribes the listener to the resource's events.
     * If $resourceName is *, then the listener will be dispatched when the specified event is fired
     * If $event is *, then the listener will be dispatched for any dispatched event of the specified resource
     * If $resourceName and $event is *, the listener will be dispatched for any dispatched event for any resource
     *
     * @param Listener $listener
     * @param String $resourceName
     * @param Mixed $event
     * @return Dispatcher
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> subscribe<span style="color: #009900;">&#40;</span>Listener <span style="color: #000088;">$listener</span><span style="color: #339933;">,</span> <span style="color: #000088;">$resourceName</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'*'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$event</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_listeners<span style="color: #009900;">&#91;</span><span style="color: #000088;">$resourceName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$event</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">spl_object_hash</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$listener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$listener</span><span style="color: #339933;">;</span>
    	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Unsubscribes the listener from the resource's events
     *
     * @param Listener $listener
     * @param String $resourceName
     * @param Mixed $event
     * @return Dispatcher
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> unsubscribe<span style="color: #009900;">&#40;</span>Listener <span style="color: #000088;">$listener</span><span style="color: #339933;">,</span> <span style="color: #000088;">$resourceName</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'*'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$event</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_listeners<span style="color: #009900;">&#91;</span><span style="color: #000088;">$resourceName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$event</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">spl_object_hash</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$listener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Publishes an event to all the listeners listening to the specified event for the specified resource
     *
     * @param Event $event
     * @return Dispatcher
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> publish<span style="color: #009900;">&#40;</span>Event <span style="color: #000088;">$event</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    	<span style="color: #000088;">$resourceName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$event</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">resourceName</span><span style="color: #339933;">;</span>
    	<span style="color: #000088;">$eventName</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$event</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">eventName</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">//Loop through all the wildcard handlers</span>
    	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_listeners<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_listeners<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$listener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    		<span style="color: #000088;">$listener</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    	<span style="color: #009900;">&#125;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">//Dispatch wildcard Resources</span>
    	<span style="color: #666666; font-style: italic;">//These are events that are published no matter what the resource</span>
    	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_listeners<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    	<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_listeners<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$event</span> <span style="color: #339933;">=&gt;;</span> <span style="color: #000088;">$listeners</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$event</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$eventName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    			<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$listeners</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$listener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    				<span style="color: #000088;">$listener</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    			<span style="color: #009900;">&#125;</span>
	    		<span style="color: #009900;">&#125;</span>
	    	<span style="color: #009900;">&#125;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">//Dispatch wildcard Events</span>
    	<span style="color: #666666; font-style: italic;">//these are listeners that are dispatched for a certain resource, despite the event</span>
    	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_listeners<span style="color: #009900;">&#91;</span><span style="color: #000088;">$resourceName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_listeners<span style="color: #009900;">&#91;</span><span style="color: #000088;">$resourceName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'*'</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$listener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   				<span style="color: #000088;">$handler</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    		<span style="color: #009900;">&#125;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">//Dispatch to a certain resource event</span>
    	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_listeners<span style="color: #009900;">&#91;</span><span style="color: #000088;">$resourceName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$eventName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    		<span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_handlers<span style="color: #009900;">&#91;</span><span style="color: #000088;">$resourceName</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$eventName</span><span style="color: #009900;">&#93;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$listener</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    			<span style="color: #000088;">$handler</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$event</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    		<span style="color: #009900;">&#125;</span>
    	<span style="color: #009900;">&#125;</span>
&nbsp;
    	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Listeners Attribute</h3>
<p>This associative array tracks all the listeners that are subscribed to events. It supports wildcard resources and wildcard events, allowing for a listener to be subscribed to a certain event and resource, all events of a certain resource, all events of a certain type, or all events across all resources. By having an object hash as the final key, well permit multiple listeners to be subscribed to a single event, while still maintaining the unsubscribe functionality (assuming the original object is still present).</p>
<h3>Subscribe Function</h3>
<p>The subscribe function subscribes a listener to either a specific event for a specific resource and event pairing, or some combination of wildcards.</p>
<h3>Unsubscribe Function</h3>
<p>Unsubscribes a listener from a resource&#8217;s event or wildcard.</p>
<h3>Publish Function</h3>
<p>The publish function is the workhorse of the Dispatcher class. Every time an event is pubished, the dispatcher searches through the listener array, looking first for any global wildcards (listeners that should be informed on any event), then resource wildcards (listeners that are subscribed to a certain event across all resources), then event wildcards (listeners that are subcribed to all events of a certain resource), and finally events that are subscribed to only one resource&#8217;s event.</p>
<h1>Putting It Together</h1>
<p>Now that we have our three components built, it is time to put it all together. First setup the dispatcher and subscribe some listeners to a resource&#8217;s event. Next, publish the event to the dispatcher and, fingers crossed, the event should be propogated to your listeners.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dispatcher</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Dispatcher<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$echoListener</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EchoListener<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dispatcher</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">subscribe</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$echoListener</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fooResource'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'barEvent'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Should output &quot;fooResource published a barEvent&quot;</span>
<span style="color: #000088;">$dispatcher</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publish</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Event<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'fooResource'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'barEvent'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Well that was a lot of writing. There are several ways to extend this model to make it a bit more elegant and efficient (such as having the publisher of an event pass an instance of itself &#8211; this is especially useful for database models, having the subscribe rules dynamically loaded based on the resource name via, say, Zend_Plugin_Loader, having the resource name automatically filled in in much the same way that the event name can be populated); however, I hope this has given you a starting point from which to implement events in your application.</p>
<p>If you have any questions / find spelling, grammar or coding mistakes please mention them in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://dustint.com/archives/38/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
