note 8775 deleted from function.set-error-handler by sobak
| From: | sobak@php.net | Date: | Sun, 24 Aug 2014 09:36:25 +0000 |
| Subject: | note 8775 deleted from function.set-error-handler by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-200300@lists.php.net to get a copy of this message | ||
Note Submitter: j dot schriver at vindiou dot com
----
A quote from Zeev Suraski:
"The CVS version (scheduled for 4.0.2) of the function has been enhanced:
- The error handler accepts three additional arguments - the filename in which the error occured,
the line number in which the error occured, and the context of the in which the error occured (a
hash that points to the active symbol table at the place of the error)."
So, you can now do this:
function myErrorHandler ($errno, $errstr, $errfile, $errline, $errcontext) {...}
[Editor's Note:
The above phrase: "...the context of the in which the error occured (a hash that points to the
active symbol table at the place of the error)." may be difficult for new programmers to
understand.
What it means is that the last argument passed to your error handler (the $errcontext in the above
example) will contain an array that contains the value of every variable that existed in the scope
the error was triggered in.
Try the following example to see exactly how this works:
<?php
// Define a simple error handler
function error_handler ($level, $message, $file, $line, $context) {
echo <<<_END_
An error of level $level was generated in file $file on line $line.
The error message was: $message
The following variables were set in the scope that the error occurred in:
<blockquote>
_END_;
print_r ($context);
print "\n</blockquote>";
}
// Set the error handler to the error_handler() function
set_error_handler ('error_handler');
// Make a function and trigger an error
function foo () {
global $SERVER_ADMIN;
$bar = 1;
// Trigger the error in the local scope of the function
trigger_error ("Some error");
}
foo();
// Now trigger the error in global scope
trigger_error ("Some other error");
?>
-zak@php.net]