函数内的“static”关键字?
我正在查看 Drupal 7 的源代码,发现了一些我以前没有见过的东西。我在 php 手册中做了一些初步的查找,但它没有解释这些示例。
关键字static
对函数内的变量有什么作用?
function module_load_all($bootstrap = FALSE) {
static $has_run = FALSE
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它使函数在多次调用之间记住给定变量的值(示例中的
$has_run
)。您可以将其用于不同的目的,例如:
在此示例中,
if
只会执行一次。即使会发生多次调用doStuff
。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:
In this example, the
if
would only be executed once. Even if multiple calls todoStuff
would occur.到目前为止似乎没有人提到,同一类的不同实例中的静态变量保持其状态。所以编写 OOP 代码时要小心。
考虑一下:
如果您希望静态变量仅记住当前类实例的状态,则最好坚持使用类属性,如下所示:
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:
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:
给出以下示例:
第一次调用
将输出
10
,然后$v
为20
。变量$v
在函数结束后不会被垃圾回收,因为它是静态(非动态)变量。该变量将保留在其范围内,直到脚本完全结束。因此,下面的调用
将输出
20
,然后将$v
设置为15
。Given the following example:
First call of
will output
10
, then$v
to be20
. 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
will then output
20
, and then set$v
to be15
.静态的工作方式与类中的工作方式相同。该变量在函数的所有实例之间共享。在您的特定示例中,一旦运行该函数,$has_run 就会设置为 TRUE。该函数的所有未来运行都将 $has_run = TRUE。这在递归函数中特别有用(作为传递计数的替代方法)。
请参阅http://php.net/manual/en/language.variables.scope。 php
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).
See http://php.net/manual/en/language.variables.scope.php
要扩展 Yang 的答案
如果您使用静态变量扩展一个类,则单个扩展类将保存在实例之间共享的“自己的”引用静态。
输出:
http://ideone.com/W4W5Qv
其他信息由 @保罗·安德鲁斯
需要注意,从 PHP8.1 开始,情况不再如此。您现在将得到输出:
php.net/manual/en/language.variables.scope 中提到了这一点。 php:
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.
outputs:
http://ideone.com/W4W5Qv
Additional info by @Paul Andrews
To note, as of PHP8.1 this is no longer true. You will now get the output:
This is mentioned on php.net/manual/en/language.variables.scope.php :
函数中的静态变量意味着无论调用该函数多少次,都只有 1 个变量。
static variable in a function means that no matter how many times you call the function, there's only 1 variable.
在函数内部,
static
意味着在页面加载生命周期内每次调用该函数时该变量都将保留其值。因此,在您给出的示例中,如果您调用一个函数两次,如果它将
$has_run
设置为true
,那么该函数将能够知道它之前已经执行过被调用是因为当函数第二次启动时,$has_run
仍等于true
。PHP 手册中解释了在此上下文中
static
关键字的用法:http://php.net/manual/en/language.variables.scope.phpInside 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
totrue
, then the function would be able to know that it had previously been called because$has_run
would still be equal totrue
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