manual-php
Version:
253 lines (191 loc) • 4.6 kB
Markdown
# 错误处理和日志记录
* [debug_backtrace - 产生一条回溯跟踪](#debugbacktrace)
* [debug_print_backtrace - 打印一条回溯](#debugprintbacktrace)
* [error_clear_last - 清除最近一次错误](#errorclearlast)
* [error_get_last - 获取最后发生的错误](#errorgetlast)
* [error_log - 发送错误信息到某个地方](#errorlog)
* [error_reporting - 设置应该报告何种 PHP 错误](#errorreporting)
* [restore_error_handler - 还原之前的错误处理函数](#restoreerrorhandler)
* [restore_exception_handler - 恢复之前定义过的异常处理函数](#restoreexceptionhandler)
* [set_error_handler - 设置用户自定义的错误处理函数](#seterrorhandler)
* [set_exception_handler - 设置用户自定义的异常处理函数](#setexceptionhandler)
* [trigger_error - 产生一个用户级别的 error / warning / notice 信息](#triggererror)
* [user_error - 产生一个用户级别的 error / warning / notice 信息](#usererror)
## debug_backtrace
```php
/**
* Just a test function.
*
* @param void
* @return void
*/
function foo()
{
var_dump(debug_backtrace());
}
foo();
```
## debug_print_backtrace
```php
/**
* Just a test function.
*
* @param void
* @return void
*/
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
/**
* Test the error handling.
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
*/
function foo(int $errno, string $errstr, string $errfile, int $errline)
{
var_dump(__FUNCTION__);
}
/**
* Test the error handling.
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
*/
function bar(int $errno, string $errstr, string $errfile, int $errline)
{
var_dump(__FUNCTION__);
}
set_error_handler('foo');
set_error_handler('bar');
trigger_error('Notice!');
// string(3) "bar"
var_dump(restore_error_handler()); // bool(true)
trigger_error('Notice!');
// string(3) "foo"
```
## restore_exception_handler
```php
/**
* Test the exception handling.
*
* @param Exception $exception
* @return void
*/
function foo(Exception $exception)
{
var_dump(__FUNCTION__);
}
/**
* Test the exception handling.
*
* @param Exception $exception
* @return void
*/
function bar(Exception $exception)
{
var_dump(__FUNCTION__);
}
set_exception_handler('foo');
set_exception_handler('bar');
var_dump(restore_exception_handler()); // bool(true)
throw new Exception('Uncaught Exception!');
// string(3) "foo"
```
## set_error_handler
```php
/**
* Test the error handling.
*
* @param int $errno
* @param string $errstr
* @param string $errfile
* @param int $errline
* @return void
*/
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
/**
* Test the exception handling.
*
* @param Exception $exception
* @return void
*/
function foo(Exception $exception)
{
var_dump($exception->getMessage());
}
set_exception_handler('foo');
throw new Exception('Uncaught Exception!');
// string(19) "Uncaught Exception!"
```
## trigger_error
```php
trigger_error('Notice!'); // PHP Notice: Notice!
trigger_error('Notice!', E_USER_NOTICE); // PHP Notice: Notice!
trigger_error('Warning!', E_USER_WARNING); // PHP Warning: Warning!
trigger_error('Error!', E_USER_ERROR); // PHP Fatal error: Error!
```
## user_error
```php
user_error('Notice!'); // PHP Notice: Notice!
user_error('Notice!', E_USER_NOTICE); // PHP Notice: Notice!
user_error('Warning!', E_USER_WARNING); // PHP Warning: Warning!
user_error('Error!', E_USER_ERROR); // PHP Fatal error: Error!
```