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
`static` keyword inside function?
提问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 static
do 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_run
in 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 if
would only be executed once. Even if multiple calls to doStuff
would 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 $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.
将输出10
,然后$v
是20
。该变量$v
在函数结束后不会被垃圾回收,因为它是一个静态(非动态)变量。该变量将保持在其范围内,直到脚本完全结束。
Therefore, the following call of
因此,以下调用
a(15);
will then output 20
, and then set $v
to be 15
.
然后将输出20
,然后设置$v
为15
.
回答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.
静态变量只存在于局部函数作用域中,但在程序执行离开该作用域时不会失去其值。
回答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
回答by Spudley
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.
在函数内部,static
意味着在页面加载期间每次调用该函数时,该变量将保留其值。
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.
因此,在您给出的示例中,如果您调用一个函数两次,如果它设置$has_run
为true
,则该函数将能够知道它以前被调用过,因为它$has_run
仍然等于true
函数第二次启动的时间。
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
static
此处的 PHP 手册解释了此上下文中关键字的用法:http: //php.net/manual/en/language.variables.scope.php