mirror of
https://github.com/php/doc-zh.git
synced 2026-04-30 03:33:11 +02:00
5e85a93c01
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@342169 c90b9560-bf6c-de11-be94-00142212c4b1
373 lines
12 KiB
XML
373 lines
12 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: 028513e94be0922956387803e75b575973539254 Maintainer: daijie Status: ready -->
|
||
<!-- Reviewed: no -->
|
||
|
||
<refentry xml:id="function.set-error-handler" xmlns="http://docbook.org/ns/docbook">
|
||
<refnamediv>
|
||
<refname>set_error_handler</refname>
|
||
<refpurpose>设置用户自定义的错误处理函数</refpurpose>
|
||
</refnamediv>
|
||
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type>mixed</type><methodname>set_error_handler</methodname>
|
||
<methodparam><type>callable</type><parameter>error_handler</parameter></methodparam>
|
||
<methodparam choice="opt"><type>int</type><parameter>error_types</parameter><initializer>E_ALL | E_STRICT</initializer></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
设置用户的函数 (<parameter>error_handler</parameter>) 来处理脚本中出现的错误。
|
||
</para>
|
||
<para>
|
||
本函数可以用你自己定义的方式来处理运行中的错误,
|
||
例如,在应用程序中严重错误发生时,或者在特定条件下触发了一个错误(使用
|
||
<function>trigger_error</function>),你需要对数据/文件做清理回收。
|
||
</para>
|
||
<para>
|
||
重要的是要记住 <parameter>error_types</parameter> 里指定的错误类型都会绕过 PHP 标准错误处理程序,
|
||
除非回调函数返回了 &false;。
|
||
<function>error_reporting</function> 设置将不会起到作用而你的错误处理函数继续会被调用
|
||
—— 不过你仍然可以获取 <link linkend="ini.error-reporting">error_reporting</link> 的当前值,并做适当处理。
|
||
需要特别注意的是带 <link linkend="language.operators.errorcontrol">@ error-control
|
||
operator</link> 前缀的语句发生错误时,这个值会是 0。
|
||
</para>
|
||
<para>
|
||
同时注意,在需要时你有责任使用 <function>die</function>。
|
||
如果错误处理程序返回了,脚本将会继续执行发生错误的后一行。
|
||
</para>
|
||
<para>
|
||
以下级别的错误不能由用户定义的函数来处理:
|
||
<constant>E_ERROR</constant>、 <constant>E_PARSE</constant>、
|
||
<constant>E_CORE_ERROR</constant>、 <constant>E_CORE_WARNING</constant>、
|
||
<constant>E_COMPILE_ERROR</constant>、
|
||
<constant>E_COMPILE_WARNING</constant>,和在
|
||
调用 <function>set_error_handler</function> 函数所在文件中产生的大多数
|
||
<constant>E_STRICT</constant>。
|
||
</para>
|
||
<para>
|
||
如果错误发生在脚本执行之前(比如文件上传时),将不会
|
||
调用自定义的错误处理程序因为它尚未在那时注册。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>error_handler</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
以下格式的回调(callback):
|
||
可以传入 &null; 重置处理程序到默认状态。
|
||
除了可以传入函数名,还可以传入引用对象和对象方法名的数组。
|
||
</para>
|
||
<para>
|
||
<methodsynopsis>
|
||
<type>bool</type><methodname><replaceable>handler</replaceable></methodname>
|
||
<methodparam><type>int</type><parameter>errno</parameter></methodparam>
|
||
<methodparam><type>string</type><parameter>errstr</parameter></methodparam>
|
||
<methodparam choice="opt"><type>string</type><parameter>errfile</parameter></methodparam>
|
||
<methodparam choice="opt"><type>int</type><parameter>errline</parameter></methodparam>
|
||
<methodparam choice="opt"><type>array</type><parameter>errcontext</parameter></methodparam>
|
||
</methodsynopsis>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>errno</parameter></term>
|
||
<listitem>
|
||
<simpara>
|
||
第一个参数 <parameter>errno</parameter>,包含了错误的级别,是一个 integer。
|
||
</simpara>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>errstr</parameter></term>
|
||
<listitem>
|
||
<simpara>
|
||
第二个参数 <parameter>errstr</parameter>,包含了错误的信息,是一个 string。
|
||
</simpara>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>errfile</parameter></term>
|
||
<listitem>
|
||
<simpara>
|
||
第三个参数是可选的,<parameter>errfile</parameter>,
|
||
包含了发生错误的文件名,是一个 string。
|
||
</simpara>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>errline</parameter></term>
|
||
<listitem>
|
||
<simpara>
|
||
第四个参数是一个可选项, <parameter>errline</parameter>,
|
||
包含了错误发生的行号,是一个 integer。
|
||
</simpara>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>errcontext</parameter></term>
|
||
<listitem>
|
||
<simpara>
|
||
第五个可选参数, <parameter>errcontext</parameter>,
|
||
是一个指向错误发生时活动符号表的 array。
|
||
也就是说,<parameter>errcontext</parameter>
|
||
会包含错误触发处作用域内所有变量的数组。
|
||
用户的错误处理程序不应该修改错误上下文(context)。
|
||
</simpara>
|
||
<warning xmlns="http://docbook.org/ns/docbook">
|
||
<simpara>
|
||
PHP 7.2.0 后此参数被<emphasis>弃用</emphasis>了。
|
||
极其不建议依赖它。
|
||
</simpara>
|
||
</warning>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
<para>
|
||
如果函数返回 &false;,标准错误处理处理程序将会继续调用。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>error_types</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
就像<link
|
||
linkend="ini.error-reporting">error_reporting</link> 的 ini 设置能够控制错误的显示一样,
|
||
此参数能够用于屏蔽 <parameter>error_handler</parameter> 的触发。
|
||
如果没有该掩码,
|
||
无论 <link
|
||
linkend="ini.error-reporting">error_reporting</link> 是如何设置的,
|
||
<parameter>error_handler</parameter> 都会在每个错误发生时被调用。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
如果之前有定义过错误处理程序,则返回该程序名称的 string;如果是内置的错误处理程序,则返回 &null;。
|
||
如果你指定了一个无效的回调函数,同样会返回 &null;。
|
||
如果之前的错误处理程序是一个类的方法,此函数会返回一个带类和方法名的索引数组(indexed array)。
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="changelog">
|
||
&reftitle.changelog;
|
||
<para>
|
||
<informaltable>
|
||
<tgroup cols="2">
|
||
<thead>
|
||
<row>
|
||
<entry>&Version;</entry>
|
||
<entry>&Description;</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry>7.2.0</entry>
|
||
<entry>
|
||
<parameter>errcontext</parameter> 被废弃。
|
||
使用此参数时会导致 <constant>E_DEPRECATED</constant> 提醒。
|
||
</entry>
|
||
</row>
|
||
<row>
|
||
<entry>5.5.0</entry>
|
||
<entry>
|
||
<parameter>error_handler</parameter> 可接收 &null;。
|
||
</entry>
|
||
</row>
|
||
<row>
|
||
<entry>5.2.0</entry>
|
||
<entry>
|
||
错误处理器必须返回 &false; 来显示
|
||
<varname>$php_errormsg</varname>。
|
||
</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</informaltable>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<example>
|
||
<title>用 <function>set_error_handler</function> 和 <function>trigger_error</function> 进行错误处理</title>
|
||
<para>
|
||
以下示例展示了通过触发错误并以用户自定义的程序来进行内部异常的处理。
|
||
</para>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
// error handler function
|
||
function myErrorHandler($errno, $errstr, $errfile, $errline)
|
||
{
|
||
if (!(error_reporting() & $errno)) {
|
||
// This error code is not included in error_reporting, so let it fall
|
||
// through to the standard PHP error handler
|
||
return false;
|
||
}
|
||
|
||
switch ($errno) {
|
||
case E_USER_ERROR:
|
||
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
|
||
echo " Fatal error on line $errline in file $errfile";
|
||
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
|
||
echo "Aborting...<br />\n";
|
||
exit(1);
|
||
break;
|
||
|
||
case E_USER_WARNING:
|
||
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
|
||
break;
|
||
|
||
case E_USER_NOTICE:
|
||
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
|
||
break;
|
||
|
||
default:
|
||
echo "Unknown error type: [$errno] $errstr<br />\n";
|
||
break;
|
||
}
|
||
|
||
/* Don't execute PHP internal error handler */
|
||
return true;
|
||
}
|
||
|
||
// function to test the error handling
|
||
function scale_by_log($vect, $scale)
|
||
{
|
||
if (!is_numeric($scale) || $scale <= 0) {
|
||
trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
|
||
}
|
||
|
||
if (!is_array($vect)) {
|
||
trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
|
||
return null;
|
||
}
|
||
|
||
$temp = array();
|
||
foreach($vect as $pos => $value) {
|
||
if (!is_numeric($value)) {
|
||
trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
|
||
$value = 0;
|
||
}
|
||
$temp[$pos] = log($scale) * $value;
|
||
}
|
||
|
||
return $temp;
|
||
}
|
||
|
||
// set to the user defined error handler
|
||
$old_error_handler = set_error_handler("myErrorHandler");
|
||
|
||
// trigger some errors, first define a mixed array with a non-numeric item
|
||
echo "vector a\n";
|
||
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
|
||
print_r($a);
|
||
|
||
// now generate second array
|
||
echo "----\nvector b - a notice (b = log(PI) * a)\n";
|
||
/* Value at position $pos is not a number, using 0 (zero) */
|
||
$b = scale_by_log($a, M_PI);
|
||
print_r($b);
|
||
|
||
// this is trouble, we pass a string instead of an array
|
||
echo "----\nvector c - a warning\n";
|
||
/* Incorrect input vector, array of values expected */
|
||
$c = scale_by_log("not array", 2.3);
|
||
var_dump($c); // NULL
|
||
|
||
// this is a critical error, log of zero or negative number is undefined
|
||
echo "----\nvector d - fatal error\n";
|
||
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
|
||
$d = scale_by_log($a, -2.5);
|
||
var_dump($d); // Never reached
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs.similar;
|
||
<screen>
|
||
<![CDATA[
|
||
vector a
|
||
Array
|
||
(
|
||
[0] => 2
|
||
[1] => 3
|
||
[2] => foo
|
||
[3] => 5.5
|
||
[4] => 43.3
|
||
[5] => 21.11
|
||
)
|
||
----
|
||
vector b - a notice (b = log(PI) * a)
|
||
<b>My NOTICE</b> [1024] Value at position 2 is not a number, using 0 (zero)<br />
|
||
Array
|
||
(
|
||
[0] => 2.2894597716988
|
||
[1] => 3.4341896575482
|
||
[2] => 0
|
||
[3] => 6.2960143721717
|
||
[4] => 49.566804057279
|
||
[5] => 24.165247890281
|
||
)
|
||
----
|
||
vector c - a warning
|
||
<b>My WARNING</b> [512] Incorrect input vector, array of values expected<br />
|
||
NULL
|
||
----
|
||
vector d - fatal error
|
||
<b>My ERROR</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br />
|
||
Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)<br />
|
||
Aborting...<br />
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
</refsect1>
|
||
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><classname>ErrorException</classname></member>
|
||
<member><function>error_reporting</function></member>
|
||
<member><function>restore_error_handler</function></member>
|
||
<member><function>trigger_error</function></member>
|
||
<member><link linkend="errorfunc.constants">error level constants</link></member>
|
||
<member>&seealso.callback;</member>
|
||
</simplelist>
|
||
</para>
|
||
</refsect1>
|
||
</refentry>
|
||
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|