Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named anonymous functions #18163

Open
divinity76 opened this issue Mar 27, 2025 · 6 comments · May be fixed by #18167
Open

Named anonymous functions #18163

divinity76 opened this issue Mar 27, 2025 · 6 comments · May be fixed by #18167

Comments

@divinity76
Copy link
Contributor

Description

Javascript support naming anonymous functions, letting the function refer to itself for recursion without polluting the surrounding scope. And it provides better stack traces. For example:

function f(cb) {
    console.log(cb.name); // Output: "funName"
    cb(0);
}

f(function funName(i) {
    // funName is available here
    if (i < 3) {
        console.log(i);
        funName(i + 1);
    }
});
// funName is not available here.

outputting

funName
0
1
2

Would be nice if PHP could support it too. Like

function d(callable $arg) {
    $ref = new ReflectionFunction($arg);
    $name = $ref->getName();
    var_dump($name);
    $arg(0);
}

d(function funName(int $i) {
    if ($i < 3) {
        echo $i, "\n";
        funName($i + 1);
    }
});
// funName is not available here.

printing

string(7) "funName"
0
1
2

Benefits:

  • Simplified Recursion: Enables recursive anonymous functions without needing workarounds like use (&$funName) or relying on debug_backtrace() hacks.
  • Improved Debugging: Stack traces and error messages involving anonymous functions could show meaningful names instead of just "{closure}"
  • Enhanced Metadata Support: When exporting function metadata (for instance, in APIs like OpenAI's ChatGPT tools API), a named anonymous function's name could be deduced automatically via reflection, rather than having to explicitly specify "string $functionName" (already possible if anonymous functions are simply not supported, though)
@Girgias
Copy link
Member

Girgias commented Mar 27, 2025

Since 8.4 closures have better names than just "{closure}" as they refer to the file name, and line number where the closure has been defined. Moreover, closures from "real" functions, already have descriptive names. See https://3v4l.org/lJmX8.

This feature really seems just strange to me, I don't know how likely recursive anonymous functions are, and even then just improving the behaviour of the __FUNCTION__ magic constant seems like a better plan.

@TimWolla
Copy link
Member

as they refer to the file name, and line number where the closure has been defined

More specifically: They refer to the parent function + line number. Only top-level closures refer to the filename (because the file effectively is the parent function).

see https://github.com/php/php-src/blob/master/Zend/tests/closures/closure_065.phpt

@TimWolla
Copy link
Member

And for the proposal at hand, there is this RFC: https://wiki.php.net/rfc/closure_self_reference

@divinity76
Copy link
Contributor Author

divinity76 commented Mar 27, 2025

Since 8.4 closures have better names than just "{closure}" as they refer to the file name, and line number where the closure has been defined.

nice.

this RFC: https://wiki.php.net/rfc/closure_self_reference

nice. closing.

@iluuu1994
Copy link
Member

This feature really seems just strange to me, I don't know how likely recursive anonymous functions are, and even then just improving the behaviour of the FUNCTION magic constant seems like a better plan.

Closures also carry context through captured variables, so __FUNCTION__ is not a viable alternative. __CLOSURE__ might work, evaluating to ZEND_CLOSURE_OBJECT(fbc).

this RFC: https://wiki.php.net/rfc/closure_self_reference

Or this, although this looks pretty stale.

@iluuu1994
Copy link
Member

Actually, this can be solved rather simply with a Closure::getCurrent() function. Maybe that would be simple alternative to a by-ref bound variable. I'll create a PR shortly and see what internals think.

@iluuu1994 iluuu1994 reopened this Mar 28, 2025
iluuu1994 added a commit to iluuu1994/php-src that referenced this issue Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants