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

PHPdragon framework(2) 接管PHP中系统报错机制:set_exception_handler和set_error_handler函数

2013-04-14 14:06 411 查看
PHP set_exception_handler() 函数

定义和用法

set_exception_handler() 函数设置用户自定义的异常处理函数。该函数用于创建运行时期间的用户自己的异常处理方法。该函数会返回旧的异常处理程序,若失败,则返回 null。

语法

set_exception_handler(exception_function)
参数描述
error_function必需。规定未捕获的异常发生时调用的函数。该函数必须在调用 set_exception_handler() 函数之前定义。这个异常处理函数需要需要一个参数,即抛出的 exception 对象。

提示和注释

提示:在这个异常处理程序被调用后,脚本会停止执行。

例子

<?php
function myException($exception)
{
echo "<b>Exception:</b> " , $exception->getMessage();
}

set_exception_handler('myException');

throw new Exception('Uncaught Exception occurred');
?>
输出:
Exception: Uncaught Exception occurred
PHP set_error_handler() 函数

定义和用法

set_error_handler() 函数设置用户自定义的错误处理函数。该函数用于创建运行时期间的用户自己的错误处理方法。该函数会返回旧的错误处理程序,若失败,则返回 null。

语法

set_error_handler(error_function,error_types)
参数描述
error_function必需。规定发生错误时运行的函数。
error_types可选。规定在哪个错误报告级别会显示用户定义的错误。默认是 "E_ALL"。

提示和注释

提示:如果使用了该函数,会完全绕过标准的 PHP 错误处理函数,如果必要,用户定义的错误处理程序必须终止 (die() ) 脚本。注释:如果在脚本执行前发生错误,由于在那时自定义程序还没有注册,因此就不会用到这个自定义错误处理程序。

例子

<?php
//error handler function
function customError($errno, $errstr, $errfile, $errline)
{
echo "<b>Custom error:</b> [$errno] $errstr<br />";
echo " Error on line $errline in $errfile<br />";
echo "Ending Script";
die();
}

//set error handler
set_error_handler("customError");

$test=2;

//trigger error
if ($test>1)
{
trigger_error("A custom error has been triggered");
}
?>
输出:
Custom error: [1024] A custom error has been triggered
Error on line 19 in C:\webfolder\test.php
Ending Script
 trigger_error()

定义和用法

trigger_error() 函数创建用户定义的错误消息。trigger_error() 用于在用户指定的条件下触发一个错误消息。它与内建的错误处理器一同使用,也可以与由 set_error_handler() 函数创建的用户自定义函数使用。如果指定了一个不合法的错误类型,该函数返回 false,否则返回 true。

语法

trigger_error(error_message,error_types)
参数描述
error_message必需。规定错误消息。长度限制为 1024 个字符。
error_types可选。规定错误消息的错误类型。 可能的值:E_USER_ERRORE_USER_WARNINGE_USER_NOTICE

例子

<?php
$test=2;
if ($test>1)
{
trigger_error("A custom error has been triggered");
}
?>
输出:
Notice: A custom error has been triggered
in C:\webfolder\test.php on line 6
 PHP error_reporting() 函数

定义和用法

error_reporting() 设置 PHP 的报错级别并返回当前级别。

语法

error_reporting(report_level)
如果参数 level 未指定,当前报错级别将被返回。下面几项是 level 可能的值:
常量描述
1E_ERRORFatal run-time errors. Errors that can not be recovered from. Execution of the script is halted
2E_WARNINGNon-fatal run-time errors. Execution of the script is not halted
4E_PARSECompile-time parse errors. Parse errors should only be generated by the parser
8E_NOTICERun-time notices. The script found something that might be an error, but could also happen when running a script normally
16E_CORE_ERRORFatal errors at PHP startup. This is like an E_ERROR in the PHP core
32E_CORE_WARNINGNon-fatal errors at PHP startup. This is like an E_WARNING in the PHP core
64E_COMPILE_ERRORFatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine
128E_COMPILE_WARNINGNon-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine
256E_USER_ERRORFatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error()
512E_USER_WARNINGNon-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error()
1024E_USER_NOTICEUser-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error()
2048E_STRICTRun-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code
4096E_RECOVERABLE_ERRORCatchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler())
8191E_ALLAll errors and warnings, except level E_STRICT (E_STRICT will be part of E_ALL as of PHP 6.0)

例子

任意数目的以上选项都可以用“或”来连接(用 OR 或 |),这样可以报告所有需要的各级别错误。例如,下面的代码关闭了用户自定义的错误和警告,执行了某些操作,然后恢复到原始的报错级别:
<?php//禁用错误报告error_reporting(0);//报告运行时错误error_reporting(E_ERROR | E_WARNING | E_PARSE);//报告所有错误error_reporting(E_ALL);?>

PHP error_log() 函数

PHP Error 和 Logging 函数

定义和用法

error_log() 函数向服务器错误记录、文件或远程目标发送一个错误。若成功,返回 true,否则返回 false。

语法

error_log(error,type,destination,headers)
参数描述
error必需。要记录的错误消息。
type可选。规定错误记录的类型。可能的记录类型:0 - 默认。根据在 php.ini 文件中的 error_log 配置,错误被发送到服务器日志系统或文件。1 - 错误被发送到 destination 参数中的地址。只有该类型使用 headers 参数。2 - 通过 PHP debugging 连接来发送错误。该选项只在 PHP 3 中可用。3 - 错误发送到文件目标字符串。
destination可选。规定向何处发送错误消息。该参数的值依赖于 "type" 参数的值。
headers可选。只在 "type" 为 1 时使用。规定附加的头部,比如 From, Cc 以及 Bcc。由 CRLF (\r\n) 分隔。注释:在发送电子邮件时,必须包含 From 头部。可以在 php.ini 文件中或者通过此参数设置。

例子

本例发送一封带有自定义错误的电子邮件:
<?php$test=2;if ($test>1){error_log("A custom error has been triggered",1,"someone@example.com","From: webmaster@example.com");}?>
输出:
A custom error has been triggered

PHP chmod() 函数

PHP Filesystem 函数

定义和用法

chmod() 函数改变文件模式。如果成功则返回 TRUE,否则返回 FALSE。

语法

chmod(file,mode)
参数描述
file必需。规定要检查的文件。
mode可选。规定新的权限。mode 参数由 4 个数字组成:第一个数字永远是 0第二个数字规定所有者的权限第二个数字规定所有者所属的用户组的权限第四个数字规定其他所有人的权限可能的值(如需设置多个权限,请对下面的数字进行总计):1 - 执行权限2 - 写权限4 - 读权限

例子

<?php// 所有者可读写,其他人没有任何权限chmod("test.txt",0600);// 所有者可读写,其他人可读chmod("test.txt",0644);// 所有者有所有权限,其他所有人可读和执行chmod("test.txt",0755);// 所有者有所有权限,所有者所在的组可读chmod("test.txt",0740);?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: