From: gulopine at gamemusic dot org
Operating system: Windows XP
PHP version: 5.1.1
PHP Bug Type: Feature/Change Request
Bug description: Include $this in errcontext for custom error handlers
Description:
------------
When setting up a custom error handler to deal with errors triggered from
within instantiated objects, it would be extremely useful to have a copy
of the object the error was triggered from. For instance, when writing a
detailed class that will be extended by several individual classes, it
would be very useful to have access to things like
get_class($errcontext['this']) or individual members, such as an $id
member. Having this available within the context passed to the error
handler would help narrow down why the error occured.
Currently the only option for this case is to implement a custom error
trigger function in the base class, which then calls trigger_error() after
prepending the error string with the class name and any useful data. This
is an acceptable workaround if there is a particular reason $this is not
passed in the error context, but it would be far more convenient in
general for these situations, and there are likely other situations I
haven't run into yet.
Reproduce code:
---------------
function error_function($errno, $errstr, $errfile, $errline, $errcontext)
{
if(isset($errcontext['this'])) {
$class = get_class($errcontext['this']);
$id = $errcontext['this']->id;
if($id) print "Class $class (ID $id): $errstr";
print "Class $class (no ID): $errstr";
}
print $errstr;
}
set_error_handler('error_function');
class test_class {
private $id;
function __construct($id = NULL) {
$this->id = $id;
trigger_error('Test error');
}
}
$test = new test_class(5);
Expected result:
----------------
Class test_class (ID 5): Test error
Actual result:
--------------
Test error
--
Edit bug report at http://bugs.php.net/?id=35909&edit=1
--
Try a CVS snapshot (PHP 4.4):
http://bugs.php.net/fix.php?id=35909&r=trysnapshot44
Try a CVS snapshot (PHP 5.1):
http://bugs.php.net/fix.php?id=35909&r=trysnapshot51
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=35909&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=35909&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=35909&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=35909&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=35909&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=35909&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=35909&r=support
Expected behavior: http://bugs.php.net/fix.php?id=35909&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=35909&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=35909&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=35909&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=35909&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=35909&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=35909&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=35909&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=35909&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=35909&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=35909&r=mysqlcfg