|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
(
)
)
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 21 05:00:01 2025 UTC |
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);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);