Req #62325 [Com]: Debug Backtrace gives no handle to anonymous functions
| From: | michael dot vorisek at email dot cz | Date: | Fri, 10 Apr 2020 19:03:23 +0000 |
| Subject: | Req #62325 [Com]: Debug Backtrace gives no handle to anonymous functions | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226523@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62325&edit=1
ID: 62325
Comment by: michael dot vorisek at email dot cz
Reported by: anfurny22 at gmail dot com
Summary: Debug Backtrace gives no handle to anonymous
functions
Status: Open
Type: Feature/Change Request
Package: *General Issues
Operating System: any
PHP Version: any
Block user comment: N
Private report: N
New Comment:
This needs to be fixes by at least providing a location where Closure was defined - filename + line.
Test code:
$fx = (function() {
print_r(debug_backtrace());
});
(function() use($fx) {
$fx();
})();
-> currently there is no way to identify/trace the location of the called Closure.
I see there is already patch for it here.
Can someone take a look at it?
If saving the Closure object is not desired, the location needs to be always accessible in order to
be trace the actual trace output.
Previous Comments:
------------------------------------------------------------------------
[2016-06-07 10:33:54] contact at jaborandi dot org dot ru
Because of this lack of support for closure by debug_backtrace, it is impossible to get the scope of
the closure from within the closure in case when $this was changed by binding closure to something
else.
This problem remains in php 7.0.7
------------------------------------------------------------------------
[2012-06-17 11:22:19] anfurny22 at gmail dot com
I agree the key name is ambiguous, perhaps 'function_reference' instead of
'reference'.
I'm unclear what you're saying about "returning" a reflection object, please
elaborate.
------------------------------------------------------------------------
[2012-06-17 11:01:14] hanskrentel at yahoo dot de
I think the naming can be improved. Reference is quite generic, why not to return
some reflection object if asked for?
------------------------------------------------------------------------
[2012-06-14 17:25:19] anfurny22 at gmail dot com
Description:
------------
Debug backtrace traditionally gives several pieces of information about the call
stack, including file name, line number, and function name. However, with
anonymous functions it simply gives "{closure}". This makes anonymous functions
received through debug_backtrace inaccesible through reflection because there is
no reference to them.
I propose that debug_backtrace always provide an additional key "reference" which
is a reference to the actual function. This will let the function be called or
provided to the reflection API.
Test script:
---------------
<?PHP
$anon = function() {
$name = (debug_backtrace[0]['name']); // yields {closure}
$refl = new ReflectionFunction($name); // works great except with closures!
echo $refl->getDocComment(); // fails
}
$anon();
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62325&edit=1