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 可能的值:
值 | 常量 | 描述 |
---|---|---|
1 | E_ERROR | Fatal run-time errors. Errors that can not be recovered from. Execution of the script is halted |
2 | E_WARNING | Non-fatal run-time errors. Execution of the script is not halted |
4 | E_PARSE | Compile-time parse errors. Parse errors should only be generated by the parser |
8 | E_NOTICE | Run-time notices. The script found something that might be an error, but could also happen when running a script normally |
16 | E_CORE_ERROR | Fatal errors at PHP startup. This is like an E_ERROR in the PHP core |
32 | E_CORE_WARNING | Non-fatal errors at PHP startup. This is like an E_WARNING in the PHP core |
64 | E_COMPILE_ERROR | Fatal compile-time errors. This is like an E_ERROR generated by the Zend Scripting Engine |
128 | E_COMPILE_WARNING | Non-fatal compile-time errors. This is like an E_WARNING generated by the Zend Scripting Engine |
256 | E_USER_ERROR | Fatal user-generated error. This is like an E_ERROR set by the programmer using the PHP function trigger_error() |
512 | E_USER_WARNING | Non-fatal user-generated warning. This is like an E_WARNING set by the programmer using the PHP function trigger_error() |
1024 | E_USER_NOTICE | User-generated notice. This is like an E_NOTICE set by the programmer using the PHP function trigger_error() |
2048 | E_STRICT | Run-time notices. PHP suggest changes to your code to help interoperability and compatibility of the code |
4096 | E_RECOVERABLE_ERROR | Catchable fatal error. This is like an E_ERROR but can be caught by a user defined handle (see also set_error_handler()) |
8191 | E_ALL | All 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);?>
相关文章推荐
- PHP 的异常处理、错误处理:error_reporting,try-catch,trigger_error,set_error_handler,set_exception_handler,register_shutdown_function
- PHP 的异常处理、错误处理:error_reporting,try-catch,trigger_error,set_error_handler,set_exception_handler,regis
- php:关闭出错提示后用set_error_handler/set_exception_handler输出出错
- PHP7的异常处理机制,set_error_handler和set_exception_handler方法介绍
- Log4J配置后, log4j:ERROR setFile(null,true) call failed. 报错:java.io.FileNotFoundException: /log/mytest2.log (系统找不到指定的路径。)
- PHP错误处理函数set_error_handler()的用法
- PHP用set_error_handler()拦截程序中的错误
- PHP的session_set_save_handler 执行机制
- PHP中set error handler函数用法小结
- PHP之set_error_handler()函数讲解
- PHP中的错误处理set_error_handler()与trigger_error()的问题
- PHP异常处理函数set_exception_handler()的用法
- log4j:ERROR setFile(null,true) call failed.java.io.FileNotFoundException: ..\logs\2010-1-19.log (系统找不到指定的路径。)
- PHP - set_error_handler()函数
- PHP - set_exception_handler() 函数
- PHPdragon framework(1) PHP中类的自动加载机制
- PHP异常处理函数set_exception_handler()的用法
- PHP 使用回调函数(set_error_handler)处理异常和错误
- php中使用session_set_save_handler(),session入库(mysql)机制
- PHP的session_set_save_handler 执行机制