php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34986 New object field for debug_backtrace() result
Submitted: 2005-10-26 06:53 UTC Modified: 2005-10-28 22:08 UTC
From: sebastian@php.net Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5.1.0RC3 OS: Irrelevant
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: sebastian@php.net
New email:
PHP Version: OS:

 

 [2005-10-26 06:53 UTC] sebastian@php.net
Description:
------------
For some Meta Programming it would be great if debug_backtrace() could return an [object] field (in addition to [class] that contains a reference to respective object.

Reproduce code:
---------------
<?php
class A {
  private $b;

  public function __construct()  {
    $this->b = new B;
  }

  public function aMethod() {
    $this->b->bMethod();
  }
}

class B {
  public function bMethod() {
    print_r(debug_backtrace());
  }
}

$a = new A;
$a->aMethod();
?>

Expected result:
----------------
Array
(
    [0] => Array
        (
            [file] => /home/sb/test.php
            [line] => 10
            [function] => bMethod
            [class] => B
            [object] => <Object id #2>
            [type] => ->
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /home/sb/test.php
            [line] => 21
            [function] => aMethod
            [class] => A
            [object] => <Object id #1>
            [type] => ->
            [args] => Array
                (
                )

        )

)

Actual result:
--------------
Array
(
    [0] => Array
        (
            [file] => /home/sb/test.php
            [line] => 10
            [function] => bMethod
            [class] => B
            [type] => ->
            [args] => Array
                (
                )

        )

    [1] => Array
        (
            [file] => /home/sb/test.php
            [line] => 21
            [function] => aMethod
            [class] => A
            [type] => ->
            [args] => Array
                (
                )

        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-26 09:40 UTC] sebastian@php.net
Just a clarification: [object] should contain a reference to the object, not an "Object id #x" string. That is only in the example output as that is what would happen for print_r(debug_backtrace()).
 [2005-10-26 10:45 UTC] sebastian@php.net
The patch below seems to work. If it really does, this was too easy :-)

Index: zend_builtin_functions.c
===================================================================
RCS file: /repository/ZendEngine2/zend_builtin_functions.c,v
retrieving revision 1.277.2.5
diff -u -b -B -r1.277.2.5 zend_builtin_functions.c
--- zend_builtin_functions.c	16 Sep 2005 17:05:06 -0000	1.277.2.5
+++ zend_builtin_functions.c	26 Oct 2005 08:44:25 -0000
@@ -1914,6 +1914,7 @@
 					
 				}
 				add_assoc_string_ex(stack_frame, "type", sizeof("type"), "->", 1);
+				add_assoc_zval_ex(stack_frame, "object", sizeof("object"), ptr->object);
 			} else if (ptr->function_state.function->common.scope) {
 				add_assoc_string_ex(stack_frame, "class", sizeof("class"), ptr->function_state.function->common.scope->name, 1);
 				add_assoc_string_ex(stack_frame, "type", sizeof("type"), "::", 1);
 [2005-10-26 11:03 UTC] sebastian@php.net
Revised patch, takes care of reference counting (thanks to Derick for pointing this out):

Index: zend_builtin_functions.c
===================================================================
RCS file: /repository/ZendEngine2/zend_builtin_functions.c,v
retrieving revision 1.277.2.5
diff -u -b -B -r1.277.2.5 zend_builtin_functions.c
--- zend_builtin_functions.c	16 Sep 2005 17:05:06 -0000	1.277.2.5
+++ zend_builtin_functions.c	26 Oct 2005 08:56:29 -0000
@@ -1913,6 +1913,9 @@
 					add_assoc_string_ex(stack_frame, "class", sizeof("class"), class_name, dup);
 					
 				}
+				add_assoc_zval_ex(stack_frame, "object", sizeof("object"), ptr->object);
+				ptr->object->refcount++;
+
 				add_assoc_string_ex(stack_frame, "type", sizeof("type"), "->", 1);
 			} else if (ptr->function_state.function->common.scope) {
 				add_assoc_string_ex(stack_frame, "class", sizeof("class"), ptr->function_state.function->common.scope->name, 1);
 [2005-10-28 22:08 UTC] sebastian@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed May 07 11:01:29 2025 UTC