php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45351 Exception::getTrace() should return 'object' array-element
Submitted: 2008-06-24 20:23 UTC Modified: 2013-01-06 04:17 UTC
Votes:22
Avg. Score:4.3 ± 0.8
Reproduced:19 of 21 (90.5%)
Same Version:14 (73.7%)
Same OS:13 (68.4%)
From: lasse100 at planet dot nl Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.3 OS: *
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2008-06-24 20:23 UTC] lasse100 at planet dot nl
Description:
------------
As in function debug_backtrace(), the method Exception::getTrace() should return a array with a 'object' element, representing the current object.

The method Exception::getTrace() should also have a $provide_object parameter like the parameter in function debug_backtrace(), to disable the 'object'-element in the array.

Reproduce code:
---------------
<?php
class Foo {
    public $bar;
    public function __construct($bar) {
        $this->bar = $bar;
        echo 'debug_backtrace: <pre>';
        var_dump(debug_backtrace());
        echo '</pre>';
        throw new Exception('Foo throws an exception!');
    }
}
try {
    $foo = new Foo('test');
} catch (Exception $e) {
    echo 'Exception::getTrace(): <pre>';
    var_dump($e->getTrace());
    echo '</pre>';
}
?>

Expected result:
----------------
debug_backtrace:

array(1) {
  [0]=>
  array(7) {
    ["file"]=>
    string(50) "C:\server\apache\htdocs\forum\htdocs\bugreport.php"
    ["line"]=>
    int(13)
    ["function"]=>
    string(11) "__construct"
    ["class"]=>
    string(3) "Foo"
    ["object"]=>
    object(Foo)#1 (1) {
      ["bar"]=>
      string(4) "test"
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(1) {
      [0]=>
      &string(4) "test"
    }
  }
}

Exception::getTrace():

array(1) {
  [0]=>
  array(6) {
    ["file"]=>
    string(50) "C:\server\apache\htdocs\forum\htdocs\bugreport.php"
    ["line"]=>
    int(13)
    ["function"]=>
    string(11) "__construct"
    ["class"]=>
    string(3) "Foo"
    ["object"]=>
    object(Foo)#1 (1) {
      ["bar"]=>
      string(4) "test"
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(1) {
      [0]=>
      string(4) "test"
    }
  }
}

Actual result:
--------------
debug_backtrace:

array(1) {
  [0]=>
  array(7) {
    ["file"]=>
    string(50) "C:\server\apache\htdocs\forum\htdocs\bugreport.php"
    ["line"]=>
    int(13)
    ["function"]=>
    string(11) "__construct"
    ["class"]=>
    string(3) "Foo"
    ["object"]=>
    object(Foo)#1 (1) {
      ["bar"]=>
      string(4) "test"
    }
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(1) {
      [0]=>
      &string(4) "test"
    }
  }
}

Exception::getTrace():

array(1) {
  [0]=>
  array(6) {
    ["file"]=>
    string(50) "C:\server\apache\htdocs\forum\htdocs\bugreport.php"
    ["line"]=>
    int(13)
    ["function"]=>
    string(11) "__construct"
    ["class"]=>
    string(3) "Foo"
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(1) {
      [0]=>
      string(4) "test"
    }
  }
}

Patches

Exception_getTrace_object_property (last revision 2010-06-13 08:43 UTC) by jille at quis dot cx)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-20 09:18 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem
 [2010-12-20 09:18 UTC] jani@php.net
-Operating System: Irrelevant +Operating System: *
 [2012-08-16 07:38 UTC] jachym dot tousek at gmail dot com
Why is this feature not yet integrated in PHP?
 [2013-01-04 19:38 UTC] lstrojny@php.net
A test is missing for the new functionality.
 [2013-01-06 04:17 UTC] stas@php.net
Unfortunately, proposed solution breaks the fix for bug #29368, since it keeps the 
reference to the constructed option. Better solution is needed (maybe better patch 
for #29368).
 [2014-11-24 09:43 UTC] roborg at hotmail dot com
As a (hopefully) simple improvement to the current situation, would it be possible to have the class name of the object passed though?

At the moment, the "class" is the class the function is defined in, which makes sense, but if it's a base class then there's no way to find out which derived class the exception was really thrown in.

Example:
--------

<?php

class A
{
	public function foo()
	{
		throw new Exception();
	}
}

class B extends A
{
}

$b = new B();

try
{
	$b->foo();
}
catch (Exception $e)
{
	var_dump($e->getTrace());
}


Expected result:
----------------

There should be a mention of "B" in the trace somewhere - in a new property "object_class" maybe?

Actual Result:
--------------

array(1) {
  [0]=>
  array(6) {
    ["file"]=>
    string(58) "c:\test.php"
    ["line"]=>
    int(19)
    ["function"]=>
    string(3) "foo"
    ["class"]=>
    string(1) "A"
    ["type"]=>
    string(2) "->"
    ["args"]=>
    array(0) {
    }
  }
}
 
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Mon Jun 15 18:01:54 2015 UTC