Skip to content

Instantly share code, notes, and snippets.

@gossi
Created June 24, 2011 11:46
Show Gist options
  • Save gossi/1044628 to your computer and use it in GitHub Desktop.
Save gossi/1044628 to your computer and use it in GitHub Desktop.
Storing php listeners in a database
This is about storing php listeners in a database.
Listeners are typically stored on objects waiting for an event using the Observer design pattern. Looks like this:
$myListener = new MyListener();
$myRecord->addlistener($myListener);
// or
$myRecord->addListener('event.created', $myListener);
From my point of view, the listener parameter of the addListener method could handle 4 different types of listeners:
1. Functions:
function listenerHandler() {
}
$myRecord->addListener('listenerHandler');
2. Instances of classes (they may implement an interface):
class MyListener {
}
$myRecord->addListener(new MyListener());
3. Method of a class
class MyListener {
public function handleEvent() {}
}
$myRecord->addListener(array('MyListener', 'handleEvent));
// or
$myRecord->addListener('MyListener', 'handleEvent');
4. Method of an instance
// MyListener as above
$myRecord->addListener(array(new MyListener(), 'handleEvent'));
This works perfectly, depending on the addListener() implementation during the runtime. Sometimes it makes sense, to store the listeners of an object in a database and when a specific event happens, look the desired listener up in the database, add it to the Record and call it. This is extremly helpful, if you don't want to register all listeners while bootstrapping the applications as there could be too many and that will definately overload your application.
So, what's best to store them in a database? And how to distinct between those listeners? (Assuming not to save the instance of a class in the database but rather the name of the class. So, for example:
I have an Invoice and an Membership class, they respectively reflect a table in a database. The Invoice doesn't know anything about the Membership though. The Membership however has a start, end and an expire date field. So, once a new membership is generated, the start date is set. When the payment (through) invoice is coming in, the expire date is set. If the expire date is reached, the expire date is written to the end date field. The individual is a member when there is a future expire date.
Because of this, the Membership record is interessted, when the respective invoice is payed to set the expire date. The code my look like this:
$m = new Membership();
$m->setStart('today');
$m->save();
$i = new Invoice();
// do stuff to set on invoice
$i->addListener($m); // or some listener, that is connected to $m
so during the runtime of the php-script the invoice is unlikely to set payed. Therefor the listener should be stored in the database and when the invoice is payed, it's up to the listener to set the expire date for the membership.
So, how to store the listener in the database? Which parameters to call addListener? How to distinct between multiple listeners, named the same, having different purpose?
Thanks for contributing ideas.
Idea #1: Descriptor
addListener() method takes one argument. This is either an array, containing information about the listener to be called, a function name or a descriptor with a method (implementing an interface) that returns an array containing the information about the listener being called. This could all be easily stored in a database.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment