php 函数内的`static`关键字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6188994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:37:08  来源:igfitidea点击:

`static` keyword inside function?

phpfunctionstatickeyword

提问by user151841

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.

我正在查看 Drupal 7 的源代码,我发现了一些我以前从未见过的东西。我在php手册中做了一些初步查找,但没有解释这些示例。

What does the keyword staticdo to a variable inside a function?

关键字static对函数内的变量有什么作用?

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

回答by Yoshi

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

它使函数记住$has_run多次调用之间给定变量(在您的示例中)的值。

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 ifwould only be executed once. Even if multiple calls to doStuffwould occur.

在这个例子中,if只会被执行一次。即使doStuff会发生多次调用。

回答by Yang

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.

到目前为止似乎没有人提到,同一类的不同实例中的静态变量保持它们的状态。所以在编写 OOP 代码时要小心。

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

回答by mauris

Given the following example:

给出以下示例:

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

First call of

第一次调用

a(20);

will output 10, then $vto be 20. The variable $vis 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.

将输出10,然后$v20。该变量$v在函数结束后不会被垃圾回收,因为它是一个静态(非动态)变量。该变量将保持在其范围内,直到脚本完全结束。

Therefore, the following call of

因此,以下调用

a(15);

will then output 20, and then set $vto be 15.

然后将输出20,然后设置$v15.

回答by tofutim

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

静态的工作方式与在类中的工作方式相同。该变量在函数的所有实例之间共享。在您的特定示例中,一旦运行该函数,$has_run 将设置为 TRUE。该函数的所有未来运行都将具有 $has_run = TRUE。这在递归函数中特别有用(作为传递计数的替代方法)。

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

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

回答by Pwnna

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

函数中的静态变量意味着无论您调用该函数多少次,都只有 1 个变量。

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

?>

回答by Tschallacka

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

一:1
:1
一:2
:3 <-- x_repeat
一:4
:5 <-- x_repeat
:2

http://ideone.com/W4W5Qv

http://ideone.com/W4W5Qv

回答by Spudley

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

在函数内部,static意味着在页面加载期间每次调用该函数时,该变量将保留其值。

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

因此,在您给出的示例中,如果您调用一个函数两次,如果它设置$has_runtrue,则该函数将能够知道它以前被调用过,因为它$has_run仍然等于true函数第二次启动的时间。

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

static此处的 PHP 手册解释了此上下文中关键字的用法:http: //php.net/manual/en/language.variables.scope.php