|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #80517 Retain parent method name of Closure in call stack
Submitted: 2020-12-15 02:55 UTC Modified: -
From: ttijhof at wikimedia dot org Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: Irrelevant OS: Debian stable
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2020-12-15 02:55 UTC] ttijhof at wikimedia dot org
When an anonymous function is encountered on the stack, it is currently not feasible (afaik) to obtain the name of the class/method or function where the closure was declared.

My use case is a sampling profiler observing EG(current_execute_data), in which I'd like to report something more useful and stable to the end-user than file path and line number.

This information would be useful to expose via debug_backtrace() as well, although maybe that should could be a separate ticket.

Downstream code:
Downstream task:
Downstream use case: Flame graphs, such as

Test script:
class ResourceLoader {
 public function getHashes() {
  return array_map( function ( $module ) {
    return $this->getModule( $module )->getVersionHash();
  }, $moduleNames );

Expected result:
Able to construct a string like "Closure$ResourceLoader::getHashes".

Before Wikimedia upgraded to PHP 7.2, we used HHVM and its Xenon extension, which provided "faux" function name for closures that were unique over a given code base and added signifant value for developers as part of stack traces.


Note that a method could define multiple closures, in which we could append a number to them. HHVM did this by appending "#2" as needed. XHProf does something similar for nested functions, by appending "@2" to the faux function name.

Actual result:
filename: /tmp/foo.php
closure_line: 3

For closures, we currently format this as {closure:/tmp/foo.php(3)}


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-15 02:58 UTC] ttijhof at wikimedia dot org
The formatted outcome for this bug, may be of use in bug 62325 as the identifier or token for the Reflection API.

- Backtrace gives no handle to anonymous functions
 [2020-12-15 11:45 UTC]
imo, this would be useful for all closures, including those created through Closure::fromCallable().
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Mon Apr 12 17:01:24 2021 UTC