headbanner

Menu:

nigel-1

Quick Notes:

April 20th, 2008:
Ruby is all syntactic sugar once you get into it. Its not a bad thing, but one should remember that. Rails does too much at run time, and I'm missing JavaScript. Need to finish my XUL based XDebug client for PHP.

Read more...

About Nigel

Nigel is a consultant at ThoughtWorks, seeking a code free nirvana. Unfortunately he's missed the second left.

Previous Posts

- Implementing the Observer Design Pattern with PHP

- Dynamic Javascript source includes in XUL Applicat...

- Interesting PHP function scoping caveat

- Implementing a Singleton Design pattern in PHP

- Unicode Rendering in Firefox is broken.

- Questions for the Campers at BarCampMumbai2

- GTAC 2007, Selenium, and the UI as objects in test...

- Installing Xdebug for PHP on Centos 4 (server)

- Learning Javascript the right way.

Archives

- August 2007
- October 2007
- January 2008
- February 2008
- March 2008

My choice of links
worth visiting

Check out

- BookEazy
- Intermission
- Sukshma
- CodeWord
- TechCrunch

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]

Hi. You've reached Nigel.in

This site is still in its very early stages, but what you have landed on, possibly inadvertently, is the website of a certain curious character a.k.a Nigel Fernandes

The aim of this page was to serve as a landing point for those of you who want to know a little more about me. The links on the right and left will take you deeper my world, or possibly on to different exciting things.

Its a big web out there and this is just one more street for you to saunter down. I hope you like it.

Close this.

My Photo
Name:
Location: Panjim, Goa, India

Crazy, dancing, programming, Goan.. I'm a computer geek and proud to be one. I program in Java, Ruby and .Net, PHP, Javascript. A lot of my recent work has been about CSS and UI design practices for large scale websites and Agile teams. I still while away hours dreaming up a web startup.

 

Implementing the Observer Design Pattern with PHP

Saturday, March 01, 2008

If you, like me need to implement a light weight Observer framework within your PHP project, and really do not need PEAR's Event_Dispatcher, look no further. ;-)

Below is my light weight implementation of a Observer pattern for PHP.
interface Observerable {
public function attach(Observer $observer);
public function detach(Observer $observer);
public function notify();
}

interface Observer {
public function update(Payload $payload);
}

class Payload {
public $data;
public function __construct($data) {
$this->data = $data;
}
}

class Watched implements Observerable {

private $state;
private $watchers;

public function __construct(){
$this->watchers = Array();
}

public function attach (Observer $observer) {
if (array_search($observer, $this->watchers, true) === false) {
$this->watchers[] = $observer;
}
}

public function detach (Observer $observer) {
$observerOffset = array_search($observer, $this->watchers, true);
if ($observerOffset !== false) {
unset($this->watchers[$observerOffset]);
}
}

public function notify () {
foreach ($this->watchers as $observer) {
$observer->update(new Payload($this->state));
}
}

public function changeState($newState) {
$this->state = $newState;
$this->notify();
}
}

class Watcher implements Observer {

public function update (Payload $payload) {
echo $payload->data;
}
}

// Create Objects
$watched = new Watched();
$watcher1 = new Watcher();
$watcher2 = new Watcher();

// Attach Watchers
$watched->attach($watcher1);
$watched->attach($watcher2);

// Change State of Watched
$state = "foo";
$watched->changeState($state); // Causes "foo" to be printed from within Watcher1 & Watcher2

// Remove a Watcher
$watched->detach($watcher1);
$watched->changeState($state); // Causes "foo" to be printed from within Watcher1
I tend to use a slightly different naming system for most my classes. Apart from that, this small snippet of code is pretty much a standard Observer pattern implementation. Very useful when you are writing small scale PHP applications (like a Facebook App. for example).

I also tend to favor the use of a small container class for passing data into the watchers, but of course since this is PHP, you could probably just use a complex array instead.

Labels: , , ,