note 76025 added to function.set-error-handler
| From: | stepheneliotdeweyatGmailDotCom at osu1 dot php dot net | Date: | Wed, 27 Jun 2007 03:48:48 +0000 |
| Subject: | note 76025 added to function.set-error-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128011@lists.php.net to get a copy of this message | ||
The manual states:
"errcontext will contain an array of every variable that existed in the scope the error was
triggered in. User error handler must not modify error context."
But do you know WHY you must not modify the error context? It appears that errcontext is (in effect
if not literally) created by taking $GLOBALS and adding the non-global local variables as additional
entries in that array, then passing the whole thing *by reference*.
(You can prove this to be true if you set up a custom error handler and then print_r($errcontext)
within it, because $GLOBALS will be printed as a recursive array).
In other words, the language in the manual is misleading, because errcontext is NOT a copy of the
variables that existed when the error WAS triggered, but rather is a reference to the *existing LIVE
variables* in the calling script.
This includes superglobal variables like $_SERVER, $_POST, $_GET, etc., as well as all user-defined
variables in scope.
The significance of that is that if you modify errcontext, you will be modifying those other
variables, not just for the life of your error handling function, but for the life of the calling
script as well.
That doesn't matter if you plan to halt execution in your error handling function, but it will
lead to unexpected behavior if you modify $errcontext and then return to the program's normal
flow after handling the error, because the variables will stay modified. For example, if you unset
$_SERVER in your custom error handling function, it will remain unset once the function is over and
you have returned to the page that generated the error.
This should be made clearer in the manual, starting by marking errhandler with an ampersand (&)
for passage by reference in the "Parameters" section above, like so:
handler ( int $errno, string $errstr [, string $errfile [, int $errline [, array &$errcontext]]]
)
----
Server IP: 208.69.120.35
Probable Submitter: 68.163.129.98
----
Manual Page -- http://www.php.net/manual/en/function.set-error-handler.php
Edit -- https://master.php.net/note/edit/76025
Del: integrated -- https://master.php.net/note/delete/76025/integrated
Del: useless -- https://master.php.net/note/delete/76025/useless
Del: bad code -- https://master.php.net/note/delete/76025/bad+code
Del: spam -- https://master.php.net/note/delete/76025/spam
Del: non-english -- https://master.php.net/note/delete/76025/non-english
Del: in docs -- https://master.php.net/note/delete/76025/in+docs
Del: other reasons-- https://master.php.net/note/delete/76025
Reject -- https://master.php.net/note/reject/76025
Search -- https://master.php.net/manage/user-notes.php