137

I was looking at the source for Drupal 7, and I found some things I hadn't seen before. I did some initial looking in the php manual, but it didn't explain these examples.

What does the keyword static do to a variable inside a function?

function module_load_all($bootstrap = FALSE) {
    static $has_run = FALSE
2

7 Answers 7

201

It makes the function remember the value of the given variable ($has_run in your example) between multiple calls.

You could use this for different purposes, for example:

function doStuff() {
  static $cache = null;

  if ($cache === null) {
     $cache = '%heavy database stuff or something%';
  }

  // code using $cache
}

In this example, the if would only be executed once. Even if multiple calls to doStuff would occur.

9
  • 6
    Also, if the function has run once, it will not reset the value of $cache to null on later calls, right?
    – user151841
    Commented Jul 6, 2011 at 14:18
  • 7
    @user151841 $cache will only be reset between requests. So yes, it will not be reset on laters calls in the same request (or execution of the script).
    – Yoshi
    Commented Jul 6, 2011 at 14:23
  • 14
    @Muhammad because that's just what the keywords static does.
    – Yoshi
    Commented Mar 13, 2014 at 8:24
  • 3
    I believe if condition check $cache === null would be executed every time this function is called, thought not if's block code $cache = '..' would be executed.
    – Aivaras
    Commented Aug 20, 2015 at 9:00
  • 1
    what happens if the function is a method in a class, is the static variable shared between instances? Commented Feb 6, 2018 at 19:30
112

Seems like nobody mentioned so far, that static variables inside different instances of the same class remain their state. So be careful when writing OOP code.

Consider this:

class Foo
{
    public function call()
    {
        static $test = 0;

        $test++;
        echo $test . PHP_EOL; 
    }
}

$a = new Foo();
$a->call(); // 1
$a->call(); // 2
$a->call(); // 3


$b = new Foo();
$b->call(); // 4
$b->call(); // 5

If you want a static variable to remember its state only for current class instance, you'd better stick to a class property, like this:

class Bar
{
    private $test = 0;

    public function call()
    {
        $this->test++;
        echo $this->test . PHP_EOL; 
    }
}


$a = new Bar();
$a->call(); // 1
$a->call(); // 2
$a->call(); // 3


$b = new Bar();
$b->call(); // 1
$b->call(); // 2
1
  • 2
    Ouch! This has bitten me more than once. I expected the static to apply to the instance only, providing memoization; but that was wrong way of thinking, because "static" in the context of classes means for the class as a whole. Properties, methods, AND variables.
    – Geoffrey
    Commented Apr 27, 2020 at 10:57
15

Given the following example:

function a($s){
    static $v = 10;
    echo $v;
    $v = $s;
}

First call of

a(20);

will output 10, then $v to be 20. The variable $v is not garbage collected after the function ends, as it is a static (non-dynamic) variable. The variable will stay within its scope until the script totally ends.

Therefore, the following call of

a(15);

will then output 20, and then set $v to be 15.

0
11

Static works the same way as it does in a class. The variable is shared across all instances of a function. In your particular example, once the function is run, $has_run is set to TRUE. All future runs of the function will have $has_run = TRUE. This is particularly useful in recursive functions (as an alternative to passing the count).

A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

See http://php.net/manual/en/language.variables.scope.php

7

To expand on the answer of Yang

If you extend a class with static variables, the individual extended classes will hold their "own" referenced static that's shared between instances.

<?php
class base {
     function calc() {
        static $foo = 0;
        $foo++;
        return $foo;
     }
}

class one extends base {
    function e() {
        echo "one:".$this->calc().PHP_EOL;
    }
}
class two extends base {
    function p() {
        echo "two:".$this->calc().PHP_EOL;
    }
}
$x = new one();
$y = new two();
$x_repeat = new one();

$x->e();
$y->p();
$x->e();
$x_repeat->e();
$x->e();
$x_repeat->e();
$y->p();

outputs:

one:1
two:1
one:2
one:3 <-- x_repeat
one:4
one:5 <-- x_repeat
two:2

http://ideone.com/W4W5Qv

3

static variable in a function means that no matter how many times you call the function, there's only 1 variable.

<?php

class Foo{
    protected static $test = 'Foo';
    function yourstatic(){
        static $test = 0;
        $test++;
        echo $test . "\n"; 
    }

    function bar(){
        $test = 0;
        $test++;
        echo $test . "\n";
    }
}

$f = new Foo();
$f->yourstatic(); // 1
$f->yourstatic(); // 2
$f->yourstatic(); // 3
$f->bar(); // 1
$f->bar(); // 1
$f->bar(); // 1

?>
2

Inside a function, static means that the variable will retain its value each time the function is called during the life of the page load.

Therefore in the example you've given, if you call a function twice, if it set $has_run to true, then the function would be able to know that it had previously been called because $has_run would still be equal to true when the function starts the second time.

The usage of the static keyword in this context is explained in the PHP manual here: http://php.net/manual/en/language.variables.scope.php

Not the answer you're looking for? Browse other questions tagged or ask your own question.