Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you clear a static variable in PHP after recursion is finished?

So for example, I have a static variable inside a recursive function, and I want that variable to be static through out each call of the recursion, but once the recursion is finished, I want that variable to be reset so that the next time I use the recursive function it starts from scratch.

For example, we have a function:

<?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);
}
?>

We can call the function for the first time like this: someFunction(); and it will work fine. Then we call it again: someFunction(); but this time it starts with the previous value for $variable. How can we reset it after the recursion of the first time we called the function so that the second time we call it it is like starting over fresh?

like image 748
trusktr Avatar asked Apr 28 '11 02:04

trusktr


People also ask

Can we reinitialize static variable?

Note: The value of a static variable can be reinitialized wherever its scope exists.

Do static variables remain even after closing the program?

Static variables have a property of preserving their value even after they are out of their scope! Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope.

How long does a static variable last?

The space for the static variable is allocated only one time and this is used for the entirety of the program. Once this variable is declared, it exists till the program executes. So, the lifetime of a static variable is the lifetime of the program.


3 Answers

The simplest thing to do is pass the variable as an argument. I wouldnt really mess with static here.

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

As a general rule you should have to pass the arguments to the function (unless they operate on class properties within the same class)... they shouldnt be global and in the case of recursion its probably not a good idea to make them static... Treat a function like a black box... values go in... they get stuff done with/to them and a result comes out. They shouldnt be aware of things happening elsewhere. There are some exceptions, but IMO they are very few.

like image 129
prodigitalson Avatar answered Oct 16 '22 17:10

prodigitalson


Prodigitalsons answer is the best solution, but since you asked for a solution using static variables and I don't see an appropriate answer here's my solution.

Just set the static variable to null when you're done. The following will print 12345 on both calls.

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

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

Or combine this with the previous answer with an initializer:

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);

Will output:

345
45
12345
-1012345
like image 34
Bas Avatar answered Oct 16 '22 19:10

Bas


Ok, well I see prodigitalson squirreled me on the answer. Here's a demo:

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);

?>

Outputs:

5
345
45
like image 3
Jared Farrish Avatar answered Oct 16 '22 18:10

Jared Farrish