错误处理和日志记录
debug_backtrace
<?php
function foo()
{
var_dump(debug_backtrace());
}
foo();
debug_print_backtrace
<?php
function foo()
{
debug_print_backtrace();
}
foo();
error_clear_last
<?php
error_clear_last();
error_get_last
<?php
var_dump(error_get_last());
error_log
<?php
error_log('Error!', 0);
error_log('Error!', 1, 'example@example.com');
error_log('Error!', 3, __DIR__ . '/error.log');
error_reporting
<?php
error_reporting(0);
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(E_ALL);
error_reporting(-1);
error_reporting(E_ALL ^ E_NOTICE);
restore_error_handler
<?php
function foo(int $errno, string $errstr, string $errfile, int $errline)
{
var_dump(__FUNCTION__);
}
function bar(int $errno, string $errstr, string $errfile, int $errline)
{
var_dump(__FUNCTION__);
}
set_error_handler('foo');
set_error_handler('bar');
trigger_error('Notice!');
var_dump(restore_error_handler());
trigger_error('Notice!');
restore_exception_handler
<?php
function foo(Exception $exception)
{
var_dump(__FUNCTION__);
}
function bar(Exception $exception)
{
var_dump(__FUNCTION__);
}
set_exception_handler('foo');
set_exception_handler('bar');
var_dump(restore_exception_handler());
throw new Exception('Uncaught Exception!');
set_error_handler
<?php
function foo(int $errno, string $errstr, string $errfile, int $errline)
{
var_dump(compact(['errno', 'errstr', 'errfile', 'errline']));
}
set_error_handler('foo');
trigger_error('Notice!');
set_error_handler('foo', E_USER_NOTICE);
trigger_error('Notice!', E_USER_NOTICE);
set_error_handler('foo', E_USER_ERROR);
trigger_error('Error!', E_USER_NOTICE);
set_exception_handler
<?php
function foo(Exception $exception)
{
var_dump($exception->getMessage());
}
set_exception_handler('foo');
throw new Exception('Uncaught Exception!');
trigger_error
<?php
trigger_error('Notice!');
trigger_error('Notice!', E_USER_NOTICE);
trigger_error('Warning!', E_USER_WARNING);
trigger_error('Error!', E_USER_ERROR);
user_error
<?php
user_error('Notice!');
user_error('Notice!', E_USER_NOTICE);
user_error('Warning!', E_USER_WARNING);
user_error('Error!', E_USER_ERROR);