有可能有一个PHP函数同时是递归和匿名的吗?这是我试图让它工作,但它没有传递函数名。
$factorial = function( $n ) use ( $factorial ) {
if( $n <= 1 ) return 1;
return $factorial( $n - 1 ) * $n;
};
print $factorial( 5 );
我也意识到这是实现阶乘的糟糕方式,这只是一个例子。
有可能有一个PHP函数同时是递归和匿名的吗?这是我试图让它工作,但它没有传递函数名。
$factorial = function( $n ) use ( $factorial ) {
if( $n <= 1 ) return 1;
return $factorial( $n - 1 ) * $n;
};
print $factorial( 5 );
我也意识到这是实现阶乘的糟糕方式,这只是一个例子。
当前回答
为了使它工作,您需要传递$阶乘作为引用
$factorial = function( $n ) use ( &$factorial ) {
if( $n == 1 ) return 1;
return $factorial( $n - 1 ) * $n;
};
print $factorial( 5 );
其他回答
c级扩展mpyw-junks/phpext-callee提供了匿名递归,而不分配变量。
<?php
var_dump((function ($n) {
return $n < 2 ? 1 : $n * callee()($n - 1);
})(5));
// 5! = 5 * 4 * 3 * 2 * 1 = int(120)
为了使它工作,您需要传递$阶乘作为引用
$factorial = function( $n ) use ( &$factorial ) {
if( $n == 1 ) return 1;
return $factorial( $n - 1 ) * $n;
};
print $factorial( 5 );
在新版本的PHP中,你可以这样做:
$x = function($depth = 0) {
if($depth++)
return;
$this($depth);
echo "hi\n";
};
$x = $x->bindTo($x);
$x();
这可能会导致奇怪的行为。
使用匿名类(PHP 7+),不定义变量:
echo (new class {
function __invoke($n) {
return $n < 2 ? 1 : $n * $this($n - 1);
}
})(5);
你可以在PHP 7.1+中使用Y Combinator:
function Y
($le)
{return
(function ($f)
{return
$f($f);
})(function ($f) use ($le)
{return
$le(function ($x) use ($f)
{return
$f($f)($x);
});
});
}
$le =
function ($factorial)
{return
function
($n) use ($factorial)
{return
$n < 2 ? $n
: $n * $factorial($n - 1);
};
};
$factorial = Y($le);
echo $factorial(1) . PHP_EOL; // 1
echo $factorial(2) . PHP_EOL; // 2
echo $factorial(5) . PHP_EOL; // 120
玩它:https://3v4l.org/7AUn2
源代码来自:https://github.com/whitephp/the-little-phper/blob/master/src/chapter_9.php