php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79460 Backtrace does not allow to obtain/identifiy Closures
Submitted: 2020-04-08 16:06 UTC Modified: 2020-04-08 16:46 UTC
From: michael dot vorisek at email dot cz Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.4.4 OS:
Private report: No CVE-ID: None
 [2020-04-08 16:06 UTC] michael dot vorisek at email dot cz
Description:
------------
Currently Closures can not be obtained from trace and thus it is very hard to debug which closure was used/called.

This is a feature request to store the Closures in backtrace instead of strings, or for BC, provide a way how to obtain them from any trace with Closures.

Test script:
---------------
<?php

function dump_closure_details(callable $fx) {
    $r = new \ReflectionFunction($fx);
    var_dump($r->getFileName() . ':' . $r->getStartLine());
}

class Test {
    public function ex() {
        $fx = function() {
            return new \Exception();
        };
        dump_closure_details($fx);
        return $fx();
    }
    
    public function trace() {
        $fx = function() {
            return debug_backtrace();
        };
        dump_closure_details($fx);
        return $fx();
    }
}

$ex = (new Test())->ex();
var_dump($ex->getTrace()[0]['function']); // res['function'] is string...
// dump_closure_details($ex->getTrace()[0]['function']);


$trace = (new Test())->trace();
var_dump($trace[0]['function']); // res['function'] is string...
// dump_closure_details($trace[0]['function']);

// how to get the original closure from the exception trace or backtrace?

Expected result:
----------------
Closure can be obtained from trace and can be called or passed to ReflectionFunction.

Actual result:
--------------
Closure is not presented in trace and there is only "{Closure}" string.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-04-08 16:46 UTC] daverandom@php.net
-Status: Open +Status: Not a bug
 [2020-04-08 16:46 UTC] daverandom@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same.

Thank you for your interest in PHP.

The ['function'] key of a trace is always a string, indicating the name of the called function, it is therefore not correct location in the trace for this information to appear.

While I don't disagree that this would be a potentially useful and sensible thing to include in the backtrace, unfortunately I don't think it's likely to be particularly practical to include, for a number of reasons.

I'm closing this as a duplicate of https://bugs.php.net/bug.php?id=62325, the issue perhaps warrants further discussion which should be conducted there.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 21:01:30 2024 UTC