12

例如,我在递归函数中有一个静态变量,并且我希望该变量在每次递归调用中都是静态的,但是一旦递归完成,我希望重置该变量,以便下次我使用它从头开始的递归函数。

例如,我们有一个函数:

<?php
function someFunction() {
    static $variable = null;
    do stuff; change value of $variable; do stuff;
    someFunction(); # The value of $variable persists through the recursion.
    return ($variable);
}
?>

我们可以像这样第一次调用该函数:someFunction();它会正常工作。然后我们再次调用它:someFunction();但这次它从 . 的前一个值开始$variable。我们如何在第一次调用函数的递归之后重置它,以便第二次调用它就像重新开始一样?

4

5 回答 5

9

最简单的做法是将变量作为参数传递。我不会真的在这里搞乱静态。

function someFunction($value = null) {
    do stuff; change value of $value; do stuff;
    someFunction($value); # The value of $variable persists through the recursion.
    return $value;
}

作为一般规则,您应该将参数传递给函数(除非它们对同一类中的类属性进行操作)......它们不应该是全局的,并且在递归的情况下,将它们设为静态可能不是一个好主意。 .. 将函数视为黑匣子......值进入......他们用它们完成了一些事情,结果出来了。他们不应该知道其他地方发生的事情。有一些例外,但 IMO 他们很少。

于 2011-04-28T02:45:58.897 回答
5

Prodigitalsons 的答案是最好的解决方案,但是由于您要求使用静态变量的解决方案,而我没有看到合适的答案,所以这是我的解决方案。

完成后只需将静态变量设置为 null 即可。以下将在两个调用中打印 12345。

function someFunction() {
    static $variable = 0;
    $variable++;
    echo $variable;
    if ($variable < 5) someFunction();

    $returnValue = $variable;
    $variable = null;
    return $returnValue;
}
someFunction();
echo "\n";
someFunction();
echo "\n";

或者将它与前面的答案与初始化程序结合起来:

function someFunction($initValue = 0) {
    static $variable = 0;
    if($initValue !== 0) {
        $variable = $initValue;    
    }
    $variable++;
    echo $variable;
    if ($variable < 5) someFunction();

    $returnValue = $variable;
    $variable = null;
    return $returnValue;
}

someFunction(2);
echo "\n";
someFunction(3);
echo "\n";
someFunction();
echo "\n";
someFunction(-2);

将输出:

345
45
12345
-1012345
于 2014-11-28T08:59:34.617 回答
3

好的,我看到 prodigitalson 让我知道答案。这是一个演示:

http://codepad.org/4R0bZf1B

<?php
function someFunction($varset = NULL) {
    static $variable = NULL;
    if ($varset !== NULL) $variable = $varset;
    $variable++;
    echo $variable;
    if ($variable < 5) someFunction();
    return $variable;
}

someFunction(4);
echo "\n";
someFunction(2);
echo "\n";
someFunction(3);

?>

输出:

5
345
45
于 2011-04-28T02:49:07.480 回答
0

您可以使用$depth计数器:

function someFunction() {
    static $variable = null, $depth= 0;
    $depth++;

    do stuff; change value of $variable; do stuff;
    someFunction(); # The value of $variable persists through the recursion.

    $depth--;
    $temp = $variable;
    if($depth== 0){
        $variable = null;
    }
    return ($temp);
}
于 2016-09-10T10:16:28.047 回答
-1

我找到了一个解决方案:

<?php
function someFunction($clear_static = false) {
    static $variable = null;
    if ($clear_static) {
        $variable = null;
    }
    do stuff; change value of $variable; do stuff;
    someFunction(); # The value of $variable persists through the recursion.
    return ($variable);
}

someFunction(); # first manual call.
someFunction(); # second manual call, $variable has value from previous use.
someFunction(true); # third manual call, value of $variable starts like new.
?>
于 2011-04-28T02:53:04.583 回答