It is currently Thu Mar 28, 2024 4:57 pm



Reply to topic  [ 1 post ] 
PHP, classes, error_handlers and destructors 
Author Message
Felix Rex
User avatar

Joined: Fri Mar 28, 2003 6:01 pm
Posts: 16646
Location: On a slope
Reply with quote
Post PHP, classes, error_handlers and destructors
heheh... I love my subject... but anyway....

in an attempt to make a script I'm writing more resilient I decided to add error handling. This is done with the set_error_handler function... however it's not class friendly, it'll only take a function. So here's how I set it up.

first I made the error handler function
[php]
function &trapError(){
static $error_Vals = array();
if (func_num_args()==5) { // Error Event has been passed
if(func_get_arg(0) == 8 || func_get_arg(1) == 'Only variables should be assigned by reference'){
return;
}
$error_Vals[] = array{
'no' => func_get_arg(0),
'text' => func_get_arg(1),
'file' => func_get_arg(2),
'line' => func_get_arg(3),
'vars'=>func_get_arg(4));
}
if (func_num_args()==0) { // Setup call. Return reference
return $error_Vals;
}
}
[/php]
this was ripped from php.net (user comments of the set_error_handler section) and mildly adapted. If 5 arguments are passed I set a static array that has all the warnings etc though I escape error numbers of 8 or errors whose text begin with "Only variables should be assigned by reference'. The first one is the one that complains if you use a var that doesn't exist, the second complains because of this function. If I call the function by itself it returns the array of accumulated errors.

The next thing was to set the error handler, so I did that in my constructor.
[php]
class whatever{
......
public function __construct(){
//set an error handler to catch warnings
$this->handler = set_error_handler('trapError');
}
[/php]

as the script executes it slowly accumulates warnings, errors, etc that aren't fatal. Any fatal error still bombs the script of course. If I want to I can pull the warnings up to this point by calling
[php]
$error_array = &trapError();
[/php]
which gives me the array of course. I then used my destructor to pull that array and write it all to a database.
[php]
public function __destruct(){
//get errors
$error_array = &trapError();
//write them to the script log
$i=0;
foreach($error_array as $err){
unset($vardump);
$no = $this->dbsafe($err['no']);
$text = $this->dbsafe($err['text']);
$file = $this->dbsafe($err['file']);
$line = $this->dbsafe($err['line']);
foreach($err['vars'] as $vars){
$vardump .= $this->dbsafe($vars);
}

$insert = "INSERT INTO newContentScriptLog
(err_no, err_text, err_file, err_line, err_vars)
VALUES
('$no','$text','$file',$line,'$vardump')";
$this->doquery($insert);
$i++;
if($i == 20){
//20 warnings is excessive... dump out in case I caught myself in a loop
break;
}
}
}
[/php]
notice the infinite loop escaper at the bottom. I ran into a situation where the inserts were causing warnings. I guess since I'm pulling the data by reference it was automatically appending itself to the array or something so it just looped infinitely.

Of course, once the data is in a database I can do all kinds of neat correlations with it.

_________________
They who can give up essential liberty to obtain a little temporary safety, deserve neither liberty nor safety.


Wed Aug 01, 2007 11:51 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 1 post ] 

Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware.