Rewriting an anonymous function in php 7.4

半城伤御伤魂 提交于 2020-01-31 15:07:22

问题


There is the following anonymous recursive function:

$f = function($n) use (&$f) {
    return ($n == 1) ? 1 : $n * $f($n - 1);
};

echo $f(5); // 120

I try to rewrite to version 7.4, but there is an error, please tell me what I'm missing?

$f = fn($n) => ($n == 1) ? 1 : $n * $f($n - 1);
echo $f(5);

Notice: Undefined variable: f

Fatal error: Uncaught Error: Function name must be a string


回答1:


Just like Barmar said, you can't use $f from the outside scope, because when the implicit binding takes place $f is still undefined.

There is nothing stopping you from passing it later as a parameter.

$f = fn($f, $n) => $n == 1 ? 1 : $n * $f($f, $n - 1);
echo $f($f, 5); // 120

The way arrow functions work, is that during definition time they will use by-value binding of the outer scope's variables.

As already mentioned, arrow functions use by-value variable binding. This is roughly equivalent to performing a use($x) for every variable $x used inside the arrow function. - https://wiki.php.net/rfc/arrow_functions_v2

The assignment of the closure to the variable $f happens after closure's definition and the variable $f is undefined prior to it.

As far as I am aware there is no mechanism for binding by-reference when defining arrow functions.




回答2:


I don't think you can rewrite the function as an arrow function.

An arrow function will capture the value of any external variables by value at the time the function is created. But $f won't have a value until after the function is created and the variable is assigned.

The original anonymous function solves this problem by capturing a reference to the variable with use (&$f), rather than use ($f). This way, it will be able to use the updated value of the variable that results from the assignment.




回答3:


I think I just found one of the legitimate (no?) uses of $GLOBALS

$f = fn ($n) =>($n == 1) ? 1 : $n * $GLOBALS['f']($n - 1);
echo $f(5); // 120

Sidenote: what if $n < 1 ?



来源:https://stackoverflow.com/questions/58882959/rewriting-an-anonymous-function-in-php-7-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!