【问题标题】:How do I Destroy a Static Variable in PHP?如何在 PHP 中销毁静态变量?
【发布时间】:2019-05-30 16:02:01
【问题描述】:

this question 的答案说unset() 不起作用,但不清楚是什么。我有一个递归函数,它使用静态变量,但是在递归完成并返回值后,我需要重置这些变量或后续调用(当前在循环中调用递归函数)将返回错误值。

在链接的问题中,有人建议在我所做的功能之外尝试$var = NULL,但似乎没有效果。

我使用静态变量而不只是将它们写为函数的参数的原因是我不希望用户可以将参数传递给该函数的情况,因为函数的唯一参数应该由它内部提供。


我的代码

<?
    require_once("randX.php");  #"randX()" generates a random floating point number in a specified range.
    // require_once("../displayArray.php");  
    error_reporting(E_ERROR | E_WARNING | E_PARSE);

/*
*   Generates a valid, random probability distribution for a given array of elements, that can be used in conjunction with "probSelect()".
*   Input: 
        $arr: An array of elements.
        $control: A value that decides how much mass is allowed to be unilaterally dumped onto one element. A high value would permit distributions where most of the mass is concentrated on one element. 
        If an invalid value is provided, the default is used.
*   Output: An associative array where the keys are the elements in the original array, and the values are their probabilities. 
*   @param array $arr:  An array of elements for which the probability distribution would be generated. 
*   @param float $control: A variable which limits the inequality of the probability distribution.
*/
    function probGen(array $arr, float $control = 0.01) 
    {
        $control = ($control <= 1 && $control >= 0)?($control):(0.00001);   #Use the default value if an invalid number is supplied.
        static $result = [];    #Initialises $result with an empty array on first function call.
        static $max = 1;    #Initialises $max with 1 on first function call.
        foreach ($arr as $value) 
        {
            $x = randX(0, $max);    #Random probability value.
            $result[$value] = ($result[$value] + $x)??0;    #Initialise the array with 0 on first call, and on subsequent calls increment by $x to assign probability mass.
            $max -= $x;     #Ensures that the probability never sums to more than one.
        }
/*
*   After the execution of the above code, there would be some leftover probability mass. 
*   The code below adds it to a random element.
*/
        $var = array_values($arr);
        if($max <= $control)    #To limit concentration of most of the probability mass in one variable.
        {
            $result[$var[rand(0,(count($var)-1))]] += $max; #Selects a random key and adds $max to it.
            // print("<br>Sum: ".array_sum($result)."<br>");
            return $result;
        }
        else
        {
            return probGen($arr, $control); 
        }
    }
    $max = NULL;        
    unset($max);
    $result = NULL; 
    unset($result);
?>

【问题讨论】:

    标签: php recursion static


    【解决方案1】:

    这始终是在任何情况下使用static 的问题,我会将它们更改为传入的参数并具有默认值...

    function probGen(array $arr, float $control = 0.01, $result = [], $max = 1 ) 
    

    (使用适当的类型)。

    然后这些可以在您的进一步调用中沿链传递...

    return probGen($arr, $control, $result, $max);
    

    这使您可以更好地控制这些值的开头(您可以将自己的值作为默认值传递)以及能够在调用过程中重置/调整它们。

    【讨论】:

    • 注意,我考虑过这一点,但认为它可能不方便,因为它允许用户为那些他们不应该做的参数传递值。你会建议我如何实现它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多