php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69027 Backtrace does not include class
Submitted: 2015-02-11 10:20 UTC Modified: 2016-04-12 17:50 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: roborg at gmail dot com Assigned:
Status: Suspended Package: Unknown/Other Function
PHP Version: 7.0.0beta1 OS: N/A
Private report: No CVE-ID: None
 [2015-02-11 10:20 UTC] roborg at gmail dot com
Description:
------------
The backtrace (either from an exception or debug_backtrace) doesn't contain an entry for the class of the object that a method was called on - only the class that the method was defined in.

In the example below, there's even though the call is on a B object, there's no mention of B in the backtrace, which cane make debugging very difficult.

I would suggest adding an extra item to the trace, e.g. "object_class", alongside class.

Test script:
---------------
<?php

class A
{
	protected $val = 0;
	
	public function ex()
	{
		throw new Exception('Value is ' . $this->val);
	}
}

class B extends A
{
	protected $val = 1;
}

try
{
	$b = new B();
	$b->ex();
}
catch (Exception $e)
{
	print '<pre>' . print_r($e->getTrace(), true) . '</pre>';
}


Expected result:
----------------
Array
(
    [0] => Array
        (
            [file] => C:\bug.php
            [line] => 21
            [function] => ex
            [class] => A
            [object_class] => B
            [type] => ->
            [args] => Array
                (
                )

        )

)

Actual result:
--------------
Array
(
    [0] => Array
        (
            [file] => C:\bug.php
            [line] => 21
            [function] => ex
            [class] => A
            [type] => ->
            [args] => Array
                (
                )

        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-21 07:46 UTC] roborg at gmail dot com
-PHP Version: Irrelevant +PHP Version: 7.0.0beta1
 [2015-07-21 07:46 UTC] roborg at gmail dot com
This affects all PHP versions, but PHP7 seems the ideal time to add it
 [2016-04-12 17:50 UTC] krakjoe@php.net
I agree, it might be useful in some cases.

However, for the information to be provided by default in traces, we would need to change the default behaviour of the function.

This has non-obvious implications, possibly effecting things like unit testing, or some other components that rely on the format of a trace.

Here is a patch: http://pastebin.com/fKwxbBaZ

I haven't changed the default behaviour in the patch, only made it possible to get called class.

What I would suggest is that you play with the patch, and when you are happy with it, whip up an RFC: http://wiki.php.net/rfc/howto

I'm going to suspend the report, because nobody can act on this request without an RFC.
 [2016-04-12 17:50 UTC] krakjoe@php.net
-Status: Open +Status: Suspended
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC