Channels ▼

Web Development

PHP: The Power Behind Web 2.0

Source Code Accompanies This Article. Download It Now.

Buses and Observers

The other code of interest is the Event Bus and Observer classes previously mentioned. The Event Bus class lets you marshal observers for a given event. These are not browser-based events but code-based ones. You can create as many as you want and your observers will contain the code necessary to process these events when they happen. In the case of this sample application, there are only two events of interest. When we initiate a newAjax request, we want to increment the number of currently outstanding requests. The corollary to that action is that when an Ajax action completes, we want to decrement the number of outstanding events. To register the observer, we include this line in the pageLoad() method:


Now let's take a look at the EventBus.js code (available online). Whenever we want to fire an event, we call EventBus.notify() with a message (or event name) and a payload. The message is used to determine which observers to notify.

When registering an observer, you specify two parameters—an instance of the observer itself and the message to subscribe to. Outside of its constructor, RequestWatcher.js (available online) implements the single method, notify(). Notify takes the payload, in this case the number of remaining requests, and updates the output container with it. When we instantiated it in pageLoad(), the parameter we handed it was requestCount. This corresponds with the ID of a span tag in the HTML to be updated.


By selecting the right tools for the job, even a three-way mash-up like this can be thrown together quickly. In the case of FNN, we chose the Zend Framework as the base because it contains all the pieces we needed to get our service written quickly. Additionally, because of the way the Zend Framework is structured, we were able to easily pull out the pieces we wanted and use them independently.

The other piece we selected was Prototype.js. Prototype's light footprint and simple interface let us quickly put the JavaScript together.

Second, by using good object-oriented techniques and practices, the server-side piece is now a framework that can be extended to consume resources from other services by building new Proxy classes.

On the client side, the OO is a bit more difficult because of language limitations, but we adhere to OO design theory whenever possible. That lends itself to well-segregated code.

Finally, the one unspoken concept we covered—building web services, even services that consume other's services, is painless. FNN talks to a simple PHP page named "service.php," which consumes other's services, but also publishes its own service. It converts the data from our news feeds, Yahoo, and Flickr, into an easily digestible format.

With the tools we've examined, you can now easily find the unique data or service that you have and build a service around it.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.