note 64023 deleted from function.set-error-handler by cmb
| From: | cmb@php.net | Date: | Sun, 13 Oct 2019 07:35:23 +0000 |
| Subject: | note 64023 deleted from function.set-error-handler by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212591@lists.php.net to get a copy of this message | ||
Note Submitter: docey
----
if you wan't to know the current error-handler
set use something like this:
$current_errorhandler = set_error_handler("myerrorhandler");
restore_error_handler();
this will set your custom error_handler and return
the name of the previous set error handler. by
calling restore_error_handler, you remove your
custom error_handler and set the error_handler that
was set back again.
this way you can also switch back to php-default error-handler
without knowing how many error-handler already
are set, by using a loop till an empty string is
returned. because an empty string means
"no error-handler set" wich means php's default
errorhandler is set back.
like in this code below:
function restore_phperrorhandler()
{
// get current error handler.
$previous_errorhandler =
set_error_handler("myerrorhandler");
restore_error_handler();
$max_loops = 20;
$cur_loops = 0;
// if current error handler is not already php's or fails.
if(($previous_errorhandler != NULL) OR
($previous_errorhandler != "")){
while($previous_errorhandler != ""){
$cur_loops = $cur_loops + 1;
// check if not exceeding infinit loop.
if($cur_loops >= $max_loops){
trigger_error("Restoring exceeds max. loops.",
E_USER_WARNING);
return false;
}
//switch to previous handler.
restore_error_handler();
// get its name and switch to it again.
$previous_errorhandler =
set_error_handler("myerrorhandler");
restore_error_handler();
// check if getting errorhandler failed.
if($previous_errorhandler == NULL){
trigger_error("Failed to get prev. error handler",
E_USER_WARNING);
return false;
// empty string = php-default error handler
}elseif($previous_errorhandler == ""){
return true;
}
}
}else{
trigger_error("Current Error-Handler is default.",
E_USER_NOTICE);
return true;
}
}
this function will loop and return true when no
error handler is left. leaving php-default handler
in controll again. this is usefull if you want to
disable your own error-handler but be sure that
php's is handling your error's and not any other
error-handler.