restore_error_handler

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

restore_error_handler β€” ВосстанавливаСт ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° ошибок

ОписаниС

function restore_error_handler(): true

Π€ΡƒΠ½ΠΊΡ†ΠΈΡŽ Π²Ρ‹Π·Ρ‹Π²Π°ΡŽΡ‚ послС измСнСния ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° ошибок Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ set_error_handler(), Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π²Π΅Ρ€Π½ΡƒΡ‚ΡŒΡΡ ΠΊ ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰Π΅ΠΌΡƒ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΡƒ, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΌ станСт ΠΈΠ»ΠΈ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠ°Ρ функция ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΠΈ ошибок, Ссли Ρ‚Π°ΠΊΡƒΡŽ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠ»ΠΈ, ΠΈΠ»ΠΈ встроСнный ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

Π‘ΠΈΠ³Π½Π°Ρ‚ΡƒΡ€Π° Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ Π½Π΅ содСрТит ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ².

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ логичСскоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ true.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ восстановлСния ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰Π΅Π³ΠΎ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° ошибок Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠ΅ΠΉ restore_error_handler()

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ опрСдСляСт, Π²Ρ‹Π·Π²Π°Π»Π° Π»ΠΈ ΠΎΡˆΠΈΠ±ΠΊΡƒ функция unserialize(), Π° Π·Π°Ρ‚Π΅ΠΌ восстанавливаСт исходный ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ошибок.

<?php

function unserialize_handler($errno, $errstr)
{
echo
"НСдопустимоС сСриализованноС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅.\n";
}

$serialized = 'foo';
set_error_handler('unserialize_handler');
$original = unserialize($serialized);
restore_error_handler();

?>

Π Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ выполнСния ΠΏΡ€ΠΈΠ²Π΅Π΄Ρ‘Π½Π½ΠΎΠ³ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€Π°:

НСдопустимоС сСриализованноС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅.

Π‘ΠΌΠΎΡ‚Ρ€ΠΈΡ‚Π΅ Ρ‚Π°ΠΊΠΆΠ΅

  • error_reporting() - ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚, ΠΎ ΠΊΠ°ΠΊΠΈΡ… PHP-ΠΎΡˆΠΈΠ±ΠΊΠ°Ρ… ΡΠΎΠΎΠ±Ρ‰Π°Ρ‚ΡŒ
  • set_error_handler() - УстанавливаСт ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΠΈΠΉ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ ошибок
  • get_error_handler() - ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΡΠΊΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° ошибок
  • restore_exception_handler() - ВосстанавливаСт ΠΏΡ€Π΅Π΄Ρ‹Π΄ΡƒΡ‰ΡƒΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΠ° ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ
  • trigger_error() - Π“Π΅Π½Π΅Ρ€ΠΈΡ€ΡƒΠ΅Ρ‚ ΠΏΡ€Π΅Π΄ΡƒΠΏΡ€Π΅ΠΆΠ΄Π΅Π½ΠΈΠ΅, ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅ ΠΈΠ»ΠΈ сообщСниС ΠΎΠ± ошибкС Π½Π° ΡƒΡ€ΠΎΠ²Π½Π΅ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 4 notes

up
33
edgarinvillegas at hotmail dot com ΒΆ
18 years ago
Isolde is kind of wrong. The error handlers are stacked with set_error_handler(), and popped with restore_error_handler(). Here i put an example:

<?php
    mysql_connect("inexistent"); //Generate an error. The actual error handler is set by default

    function foo1() {echo "<br>Error foo1<br>";}
    function foo2() {echo "<br>Error foo2<br>";}
    function foo3() {echo "<br>Error foo3<br>";}
    
    set_error_handler("foo1");    //current error handler: foo1
    set_error_handler("foo2");    //current error handler: foo2
    set_error_handler("foo3");    //current error handler: foo3
    
    mysql_connect("inexistent");    
    restore_error_handler();        //now, current error handler: foo2
    mysql_connect("inexistent");     
    restore_error_handler();        //now, current error handler: foo1
    mysql_connect("inexistent"); 
    restore_error_handler();        //now current error handler: default handler
    mysql_connect("inexistent");
    restore_error_handler();        //now current error handler: default handler (The stack can't pop more)
?>
up
1
lsole at maresme dot net ΒΆ
22 years ago
As the docs say, restore_error_handler() revert to the *previous error handler*... even if it is the same. A bug made me set twice my custom error handler and later when I was calling restore_error_handler() to restore the built-in handler nothing seemed to happen... this puzzled me for a while!
up
0
masterada at gmail dot com ΒΆ
9 years ago
Calling restore_error_handler from within an error handler might result in unexpected behaviour:

<?php
error_reporting(0);

set_error_handler('handleError1');
trigger_error('1-stack:h1');

set_error_handler('handleError2');
trigger_error('2-stack:h1,h2');

trigger_error('6-stack:h1,h2');
trigger_error('7-stack:h1,h2');

function handleError1($code, $message, $file = '', $line = 0, $context = array())
{
    echo  __METHOD__ . ' ' . $message . PHP_EOL;
}

function handleError2($code, $message, $file = '', $line = 0, $context = array())
{
    trigger_error('3-DEFAULT'); // This will use the php's default error handler

    echo  __METHOD__ . ' ' . $message . PHP_EOL;

    set_error_handler('handleError3');
    trigger_error('4-stack:h1,h2,h3');

    restore_error_handler(); // This will restore the handleError1 instead of the default error handler
    trigger_error('5-DEFAULT');
}

function handleError3($code, $message, $file = '', $line = 0, $context = array())
{
    echo  __METHOD__ . ' ' . $message . PHP_EOL;
}

?>

The above code will output:

handleError1 1-stack:h1
handleError2 2-stack:h1,h2
handleError3 4-stack:h1,h2,h3
handleError1 5-DEFAULT
handleError1 6-stack:h1,h2
handleError1 7-stack:h1,h2

The following workaround can be used:

<?php

error_reporting(0);

set_error_handler('handleError1');
trigger_error('1-stack:h1');

set_error_handler('handleError2');
trigger_error('2-stack:h1,h2');

trigger_error('6-stack:h1,h2');
trigger_error('7-stack:h1,h2');

function handleError1($code, $message, $file = '', $line = 0, $context = array())
{
    echo __METHOD__ . ' ' . $message . PHP_EOL;
}

function handleError2($code, $message, $file = '', $line = 0, $context = [])
{
    restore_error_handler(); // This will restore the previous error handler
    set_error_handler('count', 0); // Set a dummy method for error handling, it will never be called because $error_type = 0
    try
    {
        trigger_error('3-DEFAULT');

        echo __METHOD__ . ' ' . $message . PHP_EOL;

        set_error_handler('handleError3');
        trigger_error('4-stack:h1,h2,h3');

        restore_error_handler();
        trigger_error('5-DEFAULT');
    }
    finally
    {
        restore_error_handler(); // Restore the previous error handler
        set_error_handler('handleError2'); // Set the current error handler again
    }
}

function handleError3($code, $message, $file = '', $line = 0, $context = [])
{
    echo __METHOD__ . ' ' . $message . PHP_EOL;
}
?>

which will output:

handleError1 1-stack:h1
handleError2 2-stack:h1,h2
handleError3 4-stack:h1,h2,h3
handleError2 6-stack:h1,h2
handleError3 4-stack:h1,h2,h3
handleError2 7-stack:h1,h2
handleError3 4-stack:h1,h2,h3
up
-1
TiMESPLiNTER ΒΆ
11 years ago
Works also for restoring nested error handlers:

<?php

error_reporting(E_ALL);

echo '<pre>';

set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
    echo 'ErrorHandler 1: ' , $errstr , PHP_EOL;
});

trigger_error('Error 1');

set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
    echo 'ErrorHandler 2: ' , $errstr , PHP_EOL;
});

trigger_error('Error 2');

restore_error_handler();

trigger_error('Error 3');

restore_error_handler();

trigger_error('Error 4');

?>