<?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>john wyles dot com &#187; subscribe</title>
	<atom:link href="http://johnwyles.com/tag/subscribe/feed/" rel="self" type="application/rss+xml" />
	<link>http://johnwyles.com</link>
	<description>a man a plan a blog san francisco</description>
	<lastBuildDate>Tue, 08 Jun 2010 06:02:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Simple Design for Organizing PHP Callbacks using Publish-Subscribe: Kestrel PubSub</title>
		<link>http://johnwyles.com/2010/02/03/a-simple-design-for-organizing-php-callbacks-using-publish-subscribe-kestrel-pubsub/</link>
		<comments>http://johnwyles.com/2010/02/03/a-simple-design-for-organizing-php-callbacks-using-publish-subscribe-kestrel-pubsub/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 05:31:42 +0000</pubDate>
		<dc:creator>John Wyles</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[kestrel]]></category>
		<category><![CDATA[kestrel_pubsub]]></category>
		<category><![CDATA[publish]]></category>
		<category><![CDATA[publish-subscribe]]></category>
		<category><![CDATA[publisher]]></category>
		<category><![CDATA[pubsub]]></category>
		<category><![CDATA[subscribe]]></category>
		<category><![CDATA[subscriber]]></category>

		<guid isPermaLink="false">http://johnwyles.com/?p=70</guid>
		<description><![CDATA[I&#8217;ve been rethinking an observer pattern I frequently employ and attempting to refactor the logic as a separate library.  It quickly occurred to me that the enterprise publish / subscribe (PubSub) pattern was essentially applicable to callback routines.  As a fun little experiment I decided to factor out each observable as a subscriber [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been rethinking an observer pattern I frequently employ and attempting to refactor the logic as a separate library.  It quickly occurred to me that the enterprise <a href="http://en.wikipedia.org/wiki/Publish/subscribe">publish / subscribe</a> (PubSub) pattern was essentially applicable to callback routines.  As a fun little experiment I decided to factor out each observable as a subscriber and to provide the publish and subscribe methods in a simple static class.  What resulted was <a href="http://github.com/johnwyles/Kestrel_PubSub">Kestrel PubSub</a>; a static singleton class that illustrates this simple, albeit powerful, pattern.</p>
<p>Suppose, for a minute, you have the method &#8220;postError&#8221; on the object &#8220;$client&#8221; which is of type &#8220;RESTClient&#8221; and the static method &#8220;log&#8221; on the class &#8220;Logger&#8221;.  To nicely wrap up both of these to receive a publish action denoted by the subject, say &#8220;invalid_password&#8221;, we would do the following:</p>
<pre class="brush:php">
// Setup the REST client
$client = new RESTClient('127.0.0.1', 80);

// Subscribe the REST client
Kestrel_PubSub::subscribe('invalid_password', $client, 'postError');

// Subscribe the Logger
Kestrel_PubSub::subscribe('invalid_password', 'Logger', 'log');
</pre>
<p>Later in the code where we want to publish to our subscribers, the string argument &#8216;Invalid password supplied&#8217;, we then simply do the following:</p>
<pre class="brush:php">
Kestrel_PubSub::publish('invalid_password', 'Invalid password supplied.');
</pre>
<p>That&#8217;s it!  There are a few other API methods for managing these subscriptions, but shown here is the simplest use case.  I should mention to those interested in this pattern that Matthew Weier O&#8217;Phinney&#8217;s <a href="http://weierophinney.net/matthew/archives/199-A-Simple-PHP-Publish-Subscribe-System.html">A Simple PHP Publish-Subscribe System</a> has a markedly similar implementation to this one and is worth researching depending on your needs.  The one I have shown here, <a href="http://github.com/johnwyles/Kestrel_PubSub">Kestrel PubSub</a>, is a bare-bones implementation with very little object overhead and kept intentionally simplistic.  The main difference of note between the two implementations, is that Matthew&#8217;s implementation returns a subscriber handle while mine does not.  This is a nice convenience if you expect to remove subscribers, but I have yet to come up with the use case practically.  Likewise discarding this convenience can greatly increase the simplicity and clarity.  I did decide, at the last minute, to include an unsubscribe but instead of using a handle to unsubscribe a subscriber the unscubscribe method takes the original context in which the subscriber was added to remove it from the subscribers.  I feel this is a very healthy compromise, but that could be subject to change as I continue to expand use of this pattern.</p>
<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fjohnwyles.com%2F2010%2F02%2F03%2Fa-simple-design-for-organizing-php-callbacks-using-publish-subscribe-kestrel-pubsub%2F&amp;layout=button_count&amp;&amp;width=200&amp;action=like&amp;colorscheme=light" scrolling="no" frameborder="0" allowTransparency="true" style="border:none; overflow:hidden; width:200px;height:40px;margin-top:5px;"></iframe>]]></content:encoded>
			<wfw:commentRss>http://johnwyles.com/2010/02/03/a-simple-design-for-organizing-php-callbacks-using-publish-subscribe-kestrel-pubsub/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
