I just came up with an idea for an event driven class (spinoff from an error handler), and am looking for feedback. I am sure this idea is not new for the die hards, but may be of interest to the newbies.
Here goes:
class eventHandler {
var $eventData;
function eventHandler() {
// I set the error reporting so I can get the level later (don't panic)
error_reporting(2047);
$this->eventData = array();
set_error_handler(array(&$this,"eventWriter"));
}
function eventWriter ($num, $str, $file, $line, $ctx) {
// This is why I set the error reporting level
$reporting_level = error_reporting();
// Check if event needs to be logged
if (( $num & $reporting_level ) != $num ) {
// Event does not need to be logged, return to the main script
return;
}
// Here's the good stuff
($event,$class,$method,$data) = split(":",$str);
switch ($event) {
case 'redirect':
// code redirect here
break;
case 'errormsg':
// Display error message to screen
break;
case 'whateverevent':
// Add as many event types as you want
break;
deafult:
// By default store the information to the database (however you wish to format it)
}
}
}
Forgive the use of the method name eventWriter, I couldn't think of another name off the top of my head besides the constructor (eventHandler), so I just left it the way I had it from another snippet of code I pulled it from.
Now to start the errorHandler just put this in at the top of your script:
require_once ('eventhandler.class.php');
$eh=new eventHandler();
And to signal an event:
trigger_error('errormsg:thisclass:thismethod:Error Message', E_USER_NOTICE);
If you notice in this example, I used E_USER_NOTICE, you can alternately use E_USER_WARNING if you like, and you can even get fancier in your eventWriter method if you like and determine what error number was sent and perform different actions based on that information as well. The possiblities here are endless.
I am sure some of you die hards are going to jump in with try and catch stuff, and by all means, I would love to hear other methods of accomplishing this. Undoubtedly there are many ways of doing this and similar things, and I am by no means an expert, please keep your feedback constructive.
Also note, I the format of the trigger_error string I used above is highly customizable, you can change the formatting to whatever you like and just modify how the eventWriter method parses it out and uses it.
This is just a simple example (and for that matter I haven't tested it completely, it is just in the theory stage of my mind right now). I hope it helps some of you, and I hope that you die hards can give some good feedback to improve upon this idea.
Peace Out
Aaron