2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:57:16 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<refentry xml:id="function.set-error-handler" xmlns="http://docbook.org/ns/docbook">
|
2005-04-24 15:36:00 +00:00
|
|
|
<refnamediv>
|
|
|
|
<refname>set_error_handler</refname>
|
2007-02-04 22:49:08 +00:00
|
|
|
<refpurpose>Sets a user-defined error handler function</refpurpose>
|
2005-04-24 15:36:00 +00:00
|
|
|
</refnamediv>
|
2005-04-24 23:19:35 +00:00
|
|
|
|
|
|
|
<refsect1 role="description">
|
2005-04-24 15:36:00 +00:00
|
|
|
&reftitle.description;
|
|
|
|
<methodsynopsis>
|
|
|
|
<type>mixed</type><methodname>set_error_handler</methodname>
|
2012-03-12 05:55:59 +00:00
|
|
|
<methodparam><type>callable</type><parameter>error_handler</parameter></methodparam>
|
2009-01-16 16:14:26 +00:00
|
|
|
<methodparam choice="opt"><type>int</type><parameter>error_types</parameter><initializer>E_ALL | E_STRICT</initializer></methodparam>
|
2005-04-24 15:36:00 +00:00
|
|
|
</methodsynopsis>
|
|
|
|
<para>
|
|
|
|
Sets a user function (<parameter>error_handler</parameter>) to handle
|
2005-04-24 23:19:35 +00:00
|
|
|
errors in a script.
|
2005-04-24 15:36:00 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
This function can be used for defining your own way of handling errors
|
|
|
|
during runtime, for example in applications in which you need to do
|
|
|
|
cleanup of data/files when a critical error happens, or when you need
|
|
|
|
to trigger an error under certain conditions (using
|
|
|
|
<function>trigger_error</function>).
|
|
|
|
</para>
|
|
|
|
<para>
|
2005-04-24 23:19:35 +00:00
|
|
|
It is important to remember that the standard PHP error handler is completely
|
2010-01-27 12:26:37 +00:00
|
|
|
bypassed for the error types specified by <parameter>error_types</parameter>
|
|
|
|
unless the callback function returns &false;.
|
|
|
|
<function>error_reporting</function> settings will have no effect and your
|
|
|
|
error handler will be called regardless - however you are still able to read
|
|
|
|
the current value of
|
|
|
|
<link linkend="ini.error-reporting">error_reporting</link> and act
|
|
|
|
appropriately. Of particular note is that this value will be 0 if the
|
2005-04-24 23:19:35 +00:00
|
|
|
statement that caused the error was prepended by the
|
|
|
|
<link linkend="language.operators.errorcontrol">@ error-control
|
|
|
|
operator</link>.
|
|
|
|
</para>
|
|
|
|
<para>
|
2012-04-15 21:45:34 +00:00
|
|
|
Also note that it is your responsibility to <function>die</function> if
|
2005-04-24 23:19:35 +00:00
|
|
|
necessary. If the error-handler function returns, script execution
|
|
|
|
will continue with the next statement after the one that caused an error.
|
2005-04-24 15:36:00 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
2005-04-24 23:19:35 +00:00
|
|
|
The following error types cannot be handled with a user defined
|
|
|
|
function: <constant>E_ERROR</constant>, <constant>E_PARSE</constant>,
|
|
|
|
<constant>E_CORE_ERROR</constant>, <constant>E_CORE_WARNING</constant>,
|
|
|
|
<constant>E_COMPILE_ERROR</constant>,
|
2020-02-05 20:04:00 +00:00
|
|
|
<constant>E_COMPILE_WARNING</constant> independent of where they were raised, and
|
2005-04-24 23:19:35 +00:00
|
|
|
most of <constant>E_STRICT</constant> raised in the file where
|
|
|
|
<function>set_error_handler</function> is called.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
If errors occur before the script is executed (e.g. on file uploads) the
|
|
|
|
custom error handler cannot be called since it is not registered at that
|
|
|
|
time.
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="parameters">
|
|
|
|
&reftitle.parameters;
|
|
|
|
<para>
|
2005-04-24 15:36:00 +00:00
|
|
|
<variablelist>
|
|
|
|
<varlistentry>
|
2005-04-24 23:19:35 +00:00
|
|
|
<term><parameter>error_handler</parameter></term>
|
2005-04-24 15:36:00 +00:00
|
|
|
<listitem>
|
2005-04-24 23:19:35 +00:00
|
|
|
<para>
|
2013-07-21 15:49:53 +00:00
|
|
|
A callback with the following signature.
|
2013-05-16 18:34:12 +00:00
|
|
|
&null; may be passed instead, to reset this handler to its default state.
|
2014-08-23 17:55:23 +00:00
|
|
|
Instead of a function name, an array containing an object reference
|
|
|
|
and a method name can also be supplied.
|
2005-04-24 23:19:35 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<methodsynopsis>
|
2013-05-16 18:24:33 +00:00
|
|
|
<type>bool</type><methodname><replaceable>handler</replaceable></methodname>
|
2005-04-24 23:19:35 +00:00
|
|
|
<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>
|
|
|
|
The first parameter, <parameter>errno</parameter>, contains the
|
|
|
|
level of the error raised, as an integer.
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>errstr</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
The second parameter, <parameter>errstr</parameter>, contains the
|
|
|
|
error message, as a string.
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>errfile</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
The third parameter is optional, <parameter>errfile</parameter>,
|
|
|
|
which contains the filename that the error was raised in, as a string.
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>errline</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
The fourth parameter is optional, <parameter>errline</parameter>,
|
|
|
|
which contains the line number the error was raised at, as an integer.
|
|
|
|
</simpara>
|
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
|
|
|
<term><parameter>errcontext</parameter></term>
|
|
|
|
<listitem>
|
|
|
|
<simpara>
|
|
|
|
The fifth parameter is optional, <parameter>errcontext</parameter>,
|
|
|
|
which is an array that points to the active symbol table at the point
|
|
|
|
the error occurred. In other words, <parameter>errcontext</parameter>
|
|
|
|
will contain an array of every variable that existed in the scope the
|
|
|
|
error was triggered in.
|
2005-11-29 13:25:45 +00:00
|
|
|
User error handler must not modify error context.
|
2005-04-24 23:19:35 +00:00
|
|
|
</simpara>
|
2017-03-14 11:30:59 +00:00
|
|
|
<warning xmlns="http://docbook.org/ns/docbook">
|
|
|
|
<simpara>
|
|
|
|
This parameter has been <emphasis>DEPRECATED</emphasis> as of PHP 7.2.0.
|
|
|
|
Relying on it is highly discouraged.
|
|
|
|
</simpara>
|
|
|
|
</warning>
|
2005-04-24 23:19:35 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
2007-08-17 02:28:56 +00:00
|
|
|
<para>
|
|
|
|
If the function returns &false; then the normal error handler continues.
|
|
|
|
</para>
|
2005-04-24 15:36:00 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
<varlistentry>
|
2005-04-24 23:19:35 +00:00
|
|
|
<term><parameter>error_types</parameter></term>
|
2005-04-24 15:36:00 +00:00
|
|
|
<listitem>
|
2005-04-24 23:19:35 +00:00
|
|
|
<para>
|
|
|
|
Can be used to mask the triggering of the
|
|
|
|
<parameter>error_handler</parameter> function just like the <link
|
|
|
|
linkend="ini.error-reporting">error_reporting</link> ini setting
|
|
|
|
controls which errors are shown. Without this mask set the
|
|
|
|
<parameter>error_handler</parameter> will be called for every error
|
|
|
|
regardless to the setting of the <link
|
|
|
|
linkend="ini.error-reporting">error_reporting</link> setting.
|
|
|
|
</para>
|
2005-04-24 15:36:00 +00:00
|
|
|
</listitem>
|
|
|
|
</varlistentry>
|
|
|
|
</variablelist>
|
|
|
|
</para>
|
2005-04-24 23:19:35 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="returnvalues">
|
|
|
|
&reftitle.returnvalues;
|
|
|
|
<para>
|
2008-11-01 07:57:50 +00:00
|
|
|
Returns a string containing the previously defined error handler (if any). If
|
|
|
|
the built-in error handler is used &null; is returned. &null; is also returned
|
|
|
|
in case of an error such as an invalid callback. If the previous error handler
|
|
|
|
was a class method, this function will return an indexed array with the class
|
2008-11-01 06:11:19 +00:00
|
|
|
and the method name.
|
2005-04-24 23:19:35 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="changelog">
|
|
|
|
&reftitle.changelog;
|
|
|
|
<para>
|
|
|
|
<informaltable>
|
|
|
|
<tgroup cols="2">
|
|
|
|
<thead>
|
|
|
|
<row>
|
|
|
|
<entry>&Version;</entry>
|
|
|
|
<entry>&Description;</entry>
|
|
|
|
</row>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
2017-03-14 11:30:59 +00:00
|
|
|
<row>
|
|
|
|
<entry>7.2.0</entry>
|
|
|
|
<entry>
|
|
|
|
<parameter>errcontext</parameter> became deprecated. Usage of this parameter now emits an <constant>E_DEPRECATED</constant> notice.
|
|
|
|
</entry>
|
|
|
|
</row>
|
2013-05-16 18:34:12 +00:00
|
|
|
<row>
|
|
|
|
<entry>5.5.0</entry>
|
|
|
|
<entry>
|
|
|
|
<parameter>error_handler</parameter> now accepts &null;.
|
|
|
|
</entry>
|
|
|
|
</row>
|
2007-01-08 10:31:05 +00:00
|
|
|
<row>
|
|
|
|
<entry>5.2.0</entry>
|
|
|
|
<entry>
|
2008-08-23 15:40:25 +00:00
|
|
|
The error handler must return &false; to populate
|
|
|
|
<varname>$php_errormsg</varname>.
|
2007-01-08 10:31:05 +00:00
|
|
|
</entry>
|
|
|
|
</row>
|
2005-04-24 23:19:35 +00:00
|
|
|
</tbody>
|
|
|
|
</tgroup>
|
|
|
|
</informaltable>
|
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="examples">
|
|
|
|
&reftitle.examples;
|
2005-04-24 15:36:00 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
2005-04-24 23:19:35 +00:00
|
|
|
<title>Error handling with <function>set_error_handler</function> and <function>trigger_error</function></title>
|
|
|
|
<para>
|
|
|
|
The example below shows the handling of internal exceptions by
|
|
|
|
triggering errors and handling them with a user defined function:
|
|
|
|
</para>
|
2005-04-24 15:36:00 +00:00
|
|
|
<programlisting role="php">
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
// error handler function
|
2005-04-24 15:36:00 +00:00
|
|
|
function myErrorHandler($errno, $errstr, $errfile, $errline)
|
2004-01-15 12:43:50 +00:00
|
|
|
{
|
2010-07-25 11:20:19 +00:00
|
|
|
if (!(error_reporting() & $errno)) {
|
2017-01-17 10:39:08 +00:00
|
|
|
// This error code is not included in error_reporting, so let it fall
|
|
|
|
// through to the standard PHP error handler
|
|
|
|
return false;
|
2010-07-25 11:20:19 +00:00
|
|
|
}
|
|
|
|
|
2007-01-14 19:43:52 +00:00
|
|
|
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;
|
2002-04-15 00:12:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// function to test the error handling
|
2005-04-24 15:36:00 +00:00
|
|
|
function scale_by_log($vect, $scale)
|
2004-01-15 12:43:50 +00:00
|
|
|
{
|
2007-01-14 19:43:52 +00:00
|
|
|
if (!is_numeric($scale) || $scale <= 0) {
|
|
|
|
trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
|
|
|
|
}
|
2003-05-05 17:38:24 +00:00
|
|
|
|
2007-01-14 19:43:52 +00:00
|
|
|
if (!is_array($vect)) {
|
|
|
|
trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
|
|
|
|
return null;
|
|
|
|
}
|
2003-05-05 17:38:24 +00:00
|
|
|
|
2007-01-14 19:43:52 +00:00
|
|
|
$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;
|
2005-04-24 15:36:00 +00:00
|
|
|
}
|
2007-01-14 19:43:52 +00:00
|
|
|
|
2005-04-24 15:36:00 +00:00
|
|
|
return $temp;
|
2007-01-14 19:43:52 +00:00
|
|
|
}
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
// 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";
|
2007-01-14 19:43:52 +00:00
|
|
|
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
|
2002-04-15 00:12:54 +00:00
|
|
|
print_r($a);
|
|
|
|
|
2007-01-14 19:43:52 +00:00
|
|
|
// 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) */
|
2002-04-15 00:12:54 +00:00
|
|
|
$b = scale_by_log($a, M_PI);
|
|
|
|
print_r($b);
|
|
|
|
|
|
|
|
// this is trouble, we pass a string instead of an array
|
2007-01-14 19:43:52 +00:00
|
|
|
echo "----\nvector c - a warning\n";
|
|
|
|
/* Incorrect input vector, array of values expected */
|
2003-05-05 17:38:24 +00:00
|
|
|
$c = scale_by_log("not array", 2.3);
|
2007-01-14 19:43:52 +00:00
|
|
|
var_dump($c); // NULL
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
// this is a critical error, log of zero or negative number is undefined
|
|
|
|
echo "----\nvector d - fatal error\n";
|
2007-01-14 19:43:52 +00:00
|
|
|
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
|
2002-04-15 00:12:54 +00:00
|
|
|
$d = scale_by_log($a, -2.5);
|
2007-01-14 19:43:52 +00:00
|
|
|
var_dump($d); // Never reached
|
2002-04-15 00:12:54 +00:00
|
|
|
?>
|
|
|
|
]]>
|
2005-04-24 15:36:00 +00:00
|
|
|
</programlisting>
|
2005-04-24 23:19:35 +00:00
|
|
|
&example.outputs.similar;
|
2005-04-24 15:36:00 +00:00
|
|
|
<screen>
|
2002-04-15 00:12:54 +00:00
|
|
|
<![CDATA[
|
|
|
|
vector a
|
|
|
|
Array
|
|
|
|
(
|
|
|
|
[0] => 2
|
|
|
|
[1] => 3
|
|
|
|
[2] => foo
|
|
|
|
[3] => 5.5
|
|
|
|
[4] => 43.3
|
|
|
|
[5] => 21.11
|
|
|
|
)
|
|
|
|
----
|
2007-01-14 19:43:52 +00:00
|
|
|
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 />
|
2002-04-15 00:12:54 +00:00
|
|
|
Array
|
|
|
|
(
|
|
|
|
[0] => 2.2894597716988
|
|
|
|
[1] => 3.4341896575482
|
|
|
|
[2] => 0
|
|
|
|
[3] => 6.2960143721717
|
|
|
|
[4] => 49.566804057279
|
|
|
|
[5] => 24.165247890281
|
|
|
|
)
|
|
|
|
----
|
2007-01-14 19:43:52 +00:00
|
|
|
vector c - a warning
|
|
|
|
<b>My WARNING</b> [512] Incorrect input vector, array of values expected<br />
|
2002-04-15 00:12:54 +00:00
|
|
|
NULL
|
|
|
|
----
|
|
|
|
vector d - fatal error
|
2007-01-14 19:43:52 +00:00
|
|
|
<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 />
|
2003-05-05 17:38:24 +00:00
|
|
|
Aborting...<br />
|
2002-04-15 00:12:54 +00:00
|
|
|
]]>
|
2005-04-24 15:36:00 +00:00
|
|
|
</screen>
|
|
|
|
</example>
|
|
|
|
</para>
|
2005-04-24 23:19:35 +00:00
|
|
|
</refsect1>
|
|
|
|
|
|
|
|
<refsect1 role="seealso">
|
|
|
|
&reftitle.seealso;
|
2005-04-24 15:36:00 +00:00
|
|
|
<para>
|
2005-04-24 23:19:35 +00:00
|
|
|
<simplelist>
|
2008-11-15 18:22:07 +00:00
|
|
|
<member><classname>ErrorException</classname></member>
|
2005-04-24 23:19:35 +00:00
|
|
|
<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>
|
2005-04-24 15:36:00 +00:00
|
|
|
</para>
|
|
|
|
</refsect1>
|
|
|
|
</refentry>
|
2002-04-15 00:12:54 +00:00
|
|
|
|
|
|
|
<!-- 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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2002-04-15 00:12:54 +00:00
|
|
|
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
|
|
|
|
-->
|