1

I'm experimenting with the new arrow functions in PHP 7.4 and i'm trying to create a recursive arrow function one liner. Does anybody know if this is possible?

Here's my code:

$factorial = fn($x) => $x === 1 ? 1 : $x * $factorial($x - 1);

I am getting an undefined variable here. I'm assuming the inner $factorial variable does not exist yet?

Error message Notice: Undefined variable: factorial in test.php on line 10

Callum
  • 1,136
  • 3
  • 17
  • 43
  • Please ALWAYS show us the complete error message, not a summary of it – RiggsFolly Jan 31 '20 at 21:24
  • 3
    I don't think this is possible, sadly, because with normal closures [you'd need a reference pass in the `use` section](https://stackoverflow.com/questions/2480179/anonymous-recursive-php-functions), which short closures can't do. – Jeto Jan 31 '20 at 22:08
  • 1
    Does this answer your question? [Rewriting an anonymous function in php 7.4](https://stackoverflow.com/questions/58882959/rewriting-an-anonymous-function-in-php-7-4) – Rain Jan 31 '20 at 22:58

0 Answers0