您的位置:首页 > 编程语言 > PHP开发

PHP set_error_handler() 函数与 trigger_error() 函数的配合使用

2015-08-29 15:16 706 查看
两个函数的定义如下:

set_error_handler() 函数设置用户自定义的错误处理函数(注:是用户自定义的函数)

trigger_error() 函数创建用户定义的错误消息。(注:自定义的错误消息

案例如下:

<?php

function customError($errno, $errstr, $errfile,$errline){
return "<b>Custom Error:<b>[$errno]$errstr"."<br>"."Error on line $errline in $errfile";
}

class test{
public function project($test){
if(is_int($test)){
return $test;
}else{
trigger_error("Argument passed must be of type int");
}

set_error_handler("customError");
}
}

$T=new test();
echo $T->project(100);
echo $T->project("Helloworld");
?>


输出结果如下:

100

( ! ) Notice: Argument passed must be of type int in D:\www\MyProjecttest\index4.php on line 25

Call Stack

Time Memory Function Location

1 0.0000 135904 {main}( ) …\index4.php:0

2 0.0000 136384 test->project( string(10) ) …\index4.php:34

3 0.0000 136496 trigger_error ( string(35) ) …\index4.php:25
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: