PHP中错误处理的一些方法
http://tech.ddvip.com 2007年09月17日 社区交流
内容摘要:本文介绍PHP中错误处理的一些方法
在使用的时候:
set_error_handler('Error_Handler');
trigger_error('calltrigger_error',E_USER_ERROR);
这是使用函数获取的,当然您的错误报告级别应该高点:
error_reporting(1048);
为了更加oo,先看看一个描述类的自写的函数:
$classname='Exception';
/**
*(功能描述)
*@Date:
*@param (类型) (参数名) (描述)
*/
functionClassDetail($classname){
if(!class_exists($classname)){
echo($classname.'类不存在');
}else{
print_r("以下描述类".$classname);
print_r("所有的方法<pre>");
print_r(get_class_methods($classname));
print_r("</pre>");
print_r("<hr/>所有的属性<pre>");
print_r(get_class_vars($classname));
print_r("</pre><hr/>");
}
}
ClassDetail($classname);
发现exception类的所有get方法是:
[0]=>__construct
[1]=>getMessage
[2]=>getCode
[3]=>getFile
[4]=>getLine
[5]=>getTrace
[6]=>getTraceAsString
[7]=>__toString
慢慢的优化个性点,也可以的。
ok,来看比较oo的处理方式:
classErrorHandlersextendsException{
private$_context=null;
function __construct($level, $string, $file, $line, $context=null){
parent::__construct($string,$level);
$this->file = $file;
$this->line = $line;
$this->_level = $level;
$this->_context = $context;
}
function__destruct(){
// parent::__destruct();
}
functionMessage(){
$errors=array(
E_ERROR =>'error',
E_WARNING =>'warning',
E_PARSE =>'parsingerror',
E_NOTICE =>'notice',
E_CORE_ERROR =>'coreerror',
E_CORE_WARNING =>'corewarning',
E_COMPILE_ERROR =>'compileerror',
E_COMPILE_WARNING=>'compilewarning',
E_USER_ERROR =>'usererror',
E_USER_WARNING =>'userwarning',
E_USER_NOTICE =>'usernotice'
);
$str=$errors[parent::getCode()].':'.parent::getMessage().'在'.parent::getFile().
'的第'.parent::getLine()."行
";
if($this->_level==E_USER_ERROR){
$str.=('<hr/>致命错误');
}
echo('<pre>');
echo($str);
echo('</pre>');
}
}
functionerror_handler($errno,$errstr,$errorfile,$errline,$errtext){
thrownewErrorHandlers($errno,$errstr,$errorfile,$errline,$errtext);
}
functionexception_handler(Exception$e)
{
$errors=array(
E_ERROR =>'error',
E_WARNING =>'warning',
E_PARSE =>'parsingerror',
E_NOTICE =>'notice',
E_CORE_ERROR =>'coreerror',
E_CORE_WARNING =>'corewarning',
E_COMPILE_ERROR =>'compileerror',
E_COMPILE_WARNING=>'compilewarning',
E_USER_ERROR =>'usererror',
E_USER_WARNING =>'userwarning',
E_USER_NOTICE =>'usernotice');
echo$errors[$e->getCode()].':'.$e->getMessage().'in'.$e->getFile().
'online'.$e->getLine()."
";
echo$e->getTraceAsString();
}
trigger_error('5do8');
try{
$i =1/0;
}catch(ErrorHandlers $e) {
echo "发生错误.";//可以输出错误行
$e->Message();
}
责编:豆豆技术应用
- php 正则表达式
- php 入门教程
- php 安装配置
- php 函数专题
- php 函数大全(EN)
- php 5.0 中文手册
- php 4.0 中文手册
- php 程序编码规范标准
- php 常见错误
- php 中文乱码
- php Apache 安装配置
- linux php 安装配置
- windows php 安装配置
- php 十天入门教程
- php 学习笔记
- php smarty 教程
- php 分页专题
- php 类
- php 变量
- php 常量
- php 数组
- php 脚本
- php 入门实例
- php 字符串
- php.ini 配置
- php xml 专题
- php session 教程
- php 对象模型
- 更多php专题……