<?php
// 为避免污染全局命名空间,已对所有标识符加 `e` 前缀
/**
* 运行模式
* * debug 调试模式, 会开启详细的日志记录和错误提示
* * default 默认模式, 会输出错误提示
* * production 生产模式, 不会执行任何额外操作, 最高效地执行代码
*/
const eDebug = 2;
const eDefault = 1;
const eProduction = 0;
const eRunMode = eDebug;
// 默认关掉所有错误提示
error_reporting(0);
// 如果是默认模式,打开致命错误和解析错误的报告
if(eRunMode >= eDefault)
error_reporting(E_ERROR | E_PARSE);
// 如果是调试模式,打开全部错误提示,并启用修改建议提示
if(eRunMode >= eDebug)
error_reporting(E_ALL | E_STRICT);
class ePHPException extends Exception
{
protected $severity;
protected $varList;
public function __construct($message = "", $code = 0, $severity = 1, $filename = __FILE__, $lineno = __LINE__, Exception $previous = null, $varList = [])
{
$this->severity = $severity;
$this->file = $filename;
$this->line = $lineno;
$this->varList = $varList;
parent::__construct($message, $code, $previous);
}
public function getSeverity()
{
return $this->severity;
}
public function getVarList()
{
return $this->varList;
}
}
set_error_handler(function($no, $str, $file, $line, $varList)
{
throw new ePHPException($str, 0, $no, $file, $line, null, $varList);
});
if(eRunMode <= eProduction)
{
set_exception_handler(function(Exception $exception) {
header("Content-Type: text/plant; charset=UTF-8");
die(header("HTTP/1.1 500 Internal Server Error"));
});
}
else
{
set_exception_handler(function(Exception $exception) {
// 暂时我们只打算以纯文本的形式展示信息
header("Content-Type: text/plant; charset=UTF-8");
// 整体模版
$msg = "Exception `%s`: %s\nStack trace:\n%s\n thrown in %s on line %s";
// 栈的每一行的模版
$traceline = "#%s %s (%s): %s(%s)";
// 从异常对象获取运行栈
$trace = $exception->getTrace();
// 只有在调试模式才会显示参数的值,其他模式下只显示参数类型
if(eRunMode < eDebug)
foreach ($trace as $key => $stackPoint)
$trace[$key]['args'] = array_map('gettype', $trace[$key]['args']);
$result = [];
// 对运行栈的每一项应用模版
foreach ($trace as $key => $stackPoint)
$result[] = sprintf(
$traceline,
$key,
$stackPoint['file'],
$stackPoint['line'],
$stackPoint['function'],
implode(', ', $stackPoint['args'])
);
$result[] = '#' . ++$key . ' {main}';
// 应用整体模版
$msg = sprintf(
$msg,
get_class($exception),
$exception->getMessage(),
implode("\n", $result),
$exception->getFile(),
$exception->getLine()
);
// 打印运行栈等信息
print $msg;
// 如果当前是调试模式,且异常对象是我们构造的 ePHPException 类型,打印符号表
if(eRunMode >= eDebug && $exception instanceof ePHPException)
print_r($exception->getVarList());
});
}
class TypeException extends Exception{}
function repeat($argInt, $argString)
{
if(is_int($argInt) && is_int($argString))
return str_repeat($argString, $argInt);
else
throw new TypeException("Type Error");
}
function call($func, $arg1, $arg2)
{
$func($arg1, $arg2);
}
function errorFunc()
{
dontExistFunction();
}
// 错误1:
call("repeat", "Hello World", 1234);
// 错误2:
//call(new TypeException, 12, 34);
// 错误3:
//call("errorFunc", 0, 0);
- Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.36, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- Exception `TypeException`: Type Error
Stack trace:
#0 /in/SvVTS (131): repeat(Hello World, 1234)
#1 /in/SvVTS (140): call(repeat, Hello World, 1234)
#2 {main}
thrown in /in/SvVTS on line 126
- Output for 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected '[' in /in/SvVTS on line 32
Process exited with code 255. - Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
- Parse error: syntax error, unexpected T_CONST in /in/SvVTS on line 12
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected T_CONST in /in/SvVTS on line 12
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/SvVTS on line 12
Process exited with code 255.
preferences:
234.8 ms | 401 KiB | 347 Q