【问题标题】:How do you clear a static variable in PHP after recursion is finished?递归完成后如何清除 PHP 中的静态变量?
【发布时间】:2011-04-28 02:33:26
【问题描述】:

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

例如,我们有一个函数:

<?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 的前一个值开始。我们如何在第一次调用函数的递归之后重置它,以便我们第二次调用它就像重新开始一样?

【问题讨论】:

  • 我不确定我是否理解您的问题,但如果您不希望变量值通过递归持续存在,那么只需删除静态变量。
  • 我希望它通过递归持续存在...当我通过在源代码中键入手动调用函数时,我不希望它持续存在。
  • 这就是为什么我们不使用静态变量进行递归。
  • 感谢所有回答的人!这些都是很好的,但不同的方式来做同样的事情。 ;)

标签: php recursion static-variables


【解决方案1】:

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

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 很少。

【讨论】:

  • 我同意静态变量的用例很少。但是,今天我发现了一个不错的方法:在传递给preg_replace_callback 的回调中跟踪当前替换的编号。请注意,您可能需要在回调中重新初始化静态计数器,例如在退出最后一个替换之前。
【解决方案2】:

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

【讨论】:

  • 请注意,这两个示例都是“偶然”工作的,因为每次脚本执行时初始化只发生一次,但 --幸运的是 -- null$variable++ 强制转换为 0。
  • 你是对的,这正是发生的事情。在这种情况下,这不应该是公认的答案。
【解决方案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

【讨论】:

  • 哈哈。如果你住在山核桃树附近的任何地方,你就会知道松鼠总是先吃到坚果。
  • &lt;homer-simpson&gt; mmmmm 蜜饯山核桃 &lt;/homer-simpson&gt;
  • 但是如果你在没有参数的情况下调用函数,变量将永远不会重置。因此 someFunction() 的后续调用将打印 6 7 8 等...
【解决方案4】:

您可以使用$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);
}

【讨论】:

    【解决方案5】:

    我找到了解决办法:

    <?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.
    ?>
    

    【讨论】:

    • 请注意如何将关键字static 替换为关键字global 并且效果相同=(
    • 呵呵,是的。它类似于答案之一!哈哈!那么当$varset 为NULL 并且您在答案中使用++$variable 时会发生什么?
    猜你喜欢
    • 2018-01-08
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多