位置:学校首页 > 学校动态>郑州市中原工学院附近的PHP培训班
php拦截异常怎么写
php拦截异常可以通过PHP的错误、异常机制及其内建数'set_exception_handler'、'set_error_handler'、'register_shutdown_function'来写。
'set_exception_handler'函数用于拦截各种未捕获的异常,然后将这些交给用户自定义的方式进行处理
'set_error_handler'函数可以拦截各种错误,然后交给用户自定义的方式进行处理
'register_shutdown_function'函数是在PHP脚本结束时调用的函数,配合'error_get_last'可以获取较后的致命性错误
这个思路大体就是把错误、异常、致命性错误拦截下来,交给我们自定义的方法进行处理,我们辨别这些错误、异常是否致命,如果是则记录的数据库或者文件系统,然后使用脚本不停的扫描这些日志,发现严重错误立即发送邮件或发送短信进行报警
首先我们定义错误拦截类,该类用于将错误、异常拦截下来,用我们自己定义的处理方式进行处理,该类放在文件名为'errorHandler.class.php'中,代码如下
/**
*文件名称:baseErrorHandler.class.php
*摘要:错误拦截器父类
*/
require'errorHandlerException.class.php';//异常类
class errorHandler
{
public$argvs=array();
public$memoryReserveSize=262144;//备用内存大小
private$_memoryReserve;//备用内存
/**
*方法:注册自定义错误、异常拦截器
*参数:void
*返回:void
*/
public function register()
{
ini_set('display_errors',0);
set_exception_handler(array($this,'handleException'));//截获未捕获的异常
set_error_handler(array($this,'handleError'));//截获各种错误此处切不可掉换位置
//留下备用内存供后面拦截致命错误使用
$this->memoryReserveSize>0&&$this->_memoryReserve=str_repeat('x',$this->memoryReserveSize);
register_shutdown_function(array($this,'handleFatalError'));//截获致命性错误
}
/**
*方法:取消自定义错误、异常拦截器
*参数:void
*返回:void
*/
public function unregister()
{
restore_error_handler();
restore_exception_handler();
}
/**
*方法:处理截获的未捕获的异常
*参数:Exception$exception
*返回:void
*/
public function handleException($exception)
{
$this->unregister();
try
{
$this->logException($exception);
exit(1);
}
catch(Exception$e)
{
exit(1);
}
}
/**
*方法:处理截获的错误
*参数:int$code错误代码
*参数:string$message错误信息
*参数:string$file错误文件
*参数:int$line错误的行数
*返回:boolean
*/
public function handleError($code,$message,$file,$line)
{
//该处思想是将错误变成异常抛出统一交给异常处理函数进行处理
if((error_reporting()&$code)&&!in_array($code,array(E_NOTICE,E_WARNING,E_USER_NOTICE,E_USER_WARNING,E_DEPRECATED)))
{//此处只记录严重的错误对于各种WARNING NOTICE不作处理
$exception=new errorHandlerException($message,$code,$code,$file,$line);
$trace=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($trace);//trace的个元素为当前对象移除
foreach($trace as$frame)
{
if($frame['function']=='__toString')
{//如果错误出现在__toString方法中不抛出任何异常
$this->handleException($exception);
exit(1);
}
}
throw$exception;
}
return false;
}
/**
*方法:截获致命性错误
*参数:void
*返回:void
*/
public function handleFatalError()
{
unset($this->_memoryReserve);//释放内存供下面处理程序使用
$error=error_get_last();//较后一条错误信息
if(errorHandlerException::isFatalError($error))
{//如果是致命错误进行处理
$exception=new errorHandlerException($error['message'],$error['type'],$error['type'],$error['file'],$error['line']);
$this->logException($exception);
exit(1);
}
}
/**
*方法:获取服务器IP
*参数:void
*返回:string
*/
final public function getServerIp()
{
$serverIp='';
if(isset($_SERVER['SERVER_ADDR']))
{
$serverIp=$_SERVER['SERVER_ADDR'];
}
elseif(isset($_SERVER['LOCAL_ADDR']))
{
$serverIp=$_SERVER['LOCAL_ADDR'];
}
elseif(isset($_SERVER['HOSTNAME']))
{
$serverIp=gethostbyname($_SERVER['HOSTNAME']);
}
else
{
$serverIp=getenv('SERVER_ADDR');
}
return$serverIp;
}
/**
*方法:获取当前URI信息
*参数:void
*返回:string$url
*/
public function getCurrentUri()
{
$uri='';
if($_SERVER["REMOTE_ADDR"])
{//浏览器浏览模式
$uri='http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}
else
{//命令行模式
$params=$this->argvs;
$uri=$params[0];
array_shift($params);
for($i=0,$len=count($params);$i<$len;$i++)
{
$uri.=''.$params[$i];
}
}
return$uri;
}
/**
*方法:记录异常信息
*参数:errorHandlerException$e错误异常
*返回:boolean是否保存成功
*/
final public function logException($e)
{
$error=array(
'add_time'=>time(),
'title'=>errorHandlerException::getName($e->getCode()),//这里获取用户友好型名称
'message'=>array(),
'server_ip'=>$this->getServerIp(),
'code'=>errorHandlerException::getLocalCode($e->getCode()),//这里为各种错误定义一个编号以便查找
'file'=>$e->getFile(),
'line'=>$e->getLine(),
'url'=>$this->getCurrentUri(),
);
do
{
//$e->getFile().':'.$e->getLine().''.$e->getMessage().'('.$e->getCode().')'
$message=(string)$e;
$error['message'][]=$message;
}while($e=$e->getPrevious());
$error['message']=implode("\r\n",$error['message']);
$this->logError($error);
}
/**
*方法:记录异常信息
*参数:array$error=array(
*'time'=>int,
*'title'=>'string',
*'message'=>'string',
*'code'=>int,
*'server_ip'=>'string'
*'file'=>'string',
*'line'=>int,
*'url'=>'string',
*);
*返回:boolean是否保存成功
*/
public function logError($error)
{
/*这里去实现如何将错误信息记录到日志*/
}
}
上述代码中,有个'errorHandlerException'类,该类放在文件'errorHandlerException.class.php'中,该类用于将错误转换为异常,以便记录错误发生的文件、行号、错误代码、错误信息等信息,同时其方法'isFatalError'用于辨别该错误是否是致命性错误。