set_exception_handler
今天在cl框架源码中看见关于错误类的设置,其中set_exception_handler方法未操作过,顺便学习一下。
set_exception_handler:该函数设置用户自定义的异常处理函数。
<?php
function showException($exception){
echo "报错内容:".$exception->getMessage();
}
set_exception_handler('showException');//指定错误提示函数
throw new Exception('发生...程序错误');
打印内容:报错内容:发生...程序错误
在cl源码中,设置自定义报错提示,传递了数组的形式,看下示例。
set_exception_handler,如传递数组形式,第一个参数为class名,二为方法名。
注意:指定类中报错方法必须声明为静态方法。
<?php
class App{
static function customError($errno, $errstr, $errfile, $errline) {
echo "<b>自定义报错:</b> [$errno] $errstr<br />";
echo "第几行 {$errline}在{$errfile}文件<br />";
die();
}
}
set_exception_handler(array("App","customError"));
$test=2;
if ($test > 1) {
trigger_error("错误流程");
}
打印内容:
自定义报错: [1024] 错误流程
第几行 18在D:\phpstudy_pro\WWW\test\index.php文件
每天坚持,终会抵达!