Req #35909 [Opn->Wfx]: Include $this in errcontext for custom error handlers

From: Date: Sat, 23 Dec 2017 11:01:58 +0000
Subject: Req #35909 [Opn->Wfx]: Include $this in errcontext for custom error handlers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213252@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=35909&edit=1

 ID:                 35909
 Updated by:         nikic@php.net
 Reported by:        gulopine at gamemusic dot org
 Summary:            Include $this in errcontext for custom error
                     handlers
-Status:             Open
+Status:             Wont fix
 Type:               Feature/Change Request
-Package:            Feature/Change Request
+Package:            *General Issues
 Operating System:   Windows XP
 PHP Version:        5.1.1
 Block user comment: N
 Private report:     N

 New Comment:

Closing as Won't Fix, because $errcontext is being phased out. However, this information is
already available through debug_backtrace() anyway. Though I guess nowadays just using exceptions
would provide the desired information implicitly.


Previous Comments:
------------------------------------------------------------------------
[2006-01-05 18:53:32] gulopine at gamemusic dot org

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 this bug report at https://bugs.php.net/bug.php?id=35909&edit=1


Thread (2 messages)

« previous php.bugs (#213252) next »