Can someone show me some examples of anonymous recursion in PHP (if possible)?
I suppose factorial would be a good example to work with:
This is just a peck of code I created really quickly, but maybe it can help you see where I am stuck.
[code=php]
$int = 10;
$x = function($i) {
return function($i) use ($i) {
return $i != 0 ? $this($i) + $i * ($i – 1) : 0;
}
}
I can’t figure out how to do it without using an identifier (variable). Since a closure allows you to refrence $this, should I be returning
return $i != 0 ? $this($i) + $i * ($i – 1) : $i;
Don’t take my code too seriously. It doesn’t necessarily make any sense (at least, in terms of computing factorial).