php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #35909 Include $this in errcontext for custom error handlers
Submitted: 2006-01-05 18:53 UTC Modified: 2017-12-23 11:01 UTC
From: gulopine at gamemusic dot org Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.1.1 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gulopine at gamemusic dot org
New email:
PHP Version: OS:

 

 [2006-01-05 18:53 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-23 11:01 UTC] nikic@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2017-12-23 11:01 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 23:01:27 2024 UTC