php PHP中的静态变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7508284/
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 variables in PHP
提问by mike
I have found different information regarding static variables in PHP but nothing that actually explains what it is and how it really works.
我在 PHP 中发现了有关静态变量的不同信息,但没有任何内容能够真正解释它是什么以及它是如何工作的。
I have read that when used within a class that a static property cannot be used by any object instantiated by that class and that a static method can be used by an object instantiated by the class?
我已经读过,当在类中使用时,静态属性不能被该类实例化的任何对象使用,并且静态方法可以被类实例化的对象使用?
However, I have been trying to research what a static variable does within a function that is not in a class. Also, does a static variable within a function work somewhat like closure in javascript or am I totally off in this assumption?
但是,我一直在尝试研究静态变量在不在类中的函数中的作用。另外,函数中的静态变量是否有点像 javascript 中的闭包,还是我完全不同意这个假设?
回答by NullUserException
I have read that when used within a class that a static property cannot be used by any object instantiated by that class
我读过当在类中使用时,静态属性不能被该类实例化的任何对象使用
It depends on what you mean by that. eg:
这取决于你的意思。例如:
class Foo {
static $my_var = 'Foo';
}
$x = new Foo();
echo $x::$my_var; // works fine
echo $x->my_var; // doesn't work - Notice: Undefined property: Foo::$my_var
and that a static method can be used by an object instantiated by the class???
并且静态方法可以被类实例化的对象使用???
Yes, an instantiated object belonging to the class can access a static method.
是的,属于该类的实例化对象可以访问静态方法。
The keyword static
in the context of classes behave somewhat like static class variables in other languages. A member (method or variable) declared static
is associated with the class and rather than an instance of that class. Thus, you can access it without an instance of the class (eg: in the example above, I could use Foo::$my_var
)
static
类上下文中的关键字的行为有点像其他语言中的静态类变量。声明的成员(方法或变量)static
与类相关联,而不是与该类的实例相关联。因此,您可以在没有类实例的情况下访问它(例如:在上面的示例中,我可以使用Foo::$my_var
)
However, I have been trying to research what a static variable does within a function that is not in a class.
Also, does a static variable within a function work somewhat like closure in javascript or am I totally off in this assumption.
但是,我一直在尝试研究静态变量在不在类中的函数中的作用。
此外,函数中的静态变量是否有点像 javascript 中的闭包,或者我完全不同意这个假设。
Outside of classes (ie: in functions), a static
variable is a variable that doesn't lose its value when the function exits. So in sense, yes, they work like closures in JavaScript.
在类之外(即:在函数中),static
变量是在函数退出时不会丢失其值的变量。所以从某种意义上说,是的,它们就像 JavaScript 中的闭包一样工作。
But unlike JS closures, there's only one value for the variable that's maintained across different invocations of the same function. From the PHP manual's example:
但与 JS 闭包不同的是,在同一函数的不同调用中,变量只有一个值。来自 PHP 手册的示例:
function test()
{
static $a = 0;
echo $a;
$a++;
}
test(); // prints 0
test(); // prints 1
test(); // prints 2
Reference: static
keyword (in classes), (in functions)
参考:static
关键字(在类中),(在函数中)
回答by connec
static
has two uses in PHP:
static
在 PHP 中有两个用途:
First, and most commonly, it can be used to define 'class' variables/functions (as opposed to instance variables/functions), that can be accessed without instantiating a class:
首先,也是最常见的,它可用于定义“类”变量/函数(与实例变量/函数相反),无需实例化即可访问这些变量/函数:
class A {
public static $var = 'val'; // $var is static (in class context)
public $other_var = 'other_val'; // non-static
}
echo A::$var; // val
echo A::$other_var // doesn't work (fatal error, undefined static variable)
$a = new A;
echo $a->var // won't work (strict standards)
echo $a->other_var // other_val
Secondly, it can be used to maintain state between function calls:
其次,它可以用来维护函数调用之间的状态:
function a() {
static $i = 0;
$j = 0;
return array($i++, $j++);
}
print_r(a()); // array(0, 0)
print_r(a()); // array(1, 0)
print_r(a()); // array(2, 0)
//...
Note that declaring a variable static within a function works the same regardless of whether or not the function is defined in a class, all that matters is where the variableis declared (class member or in a function).
请注意,无论函数是否在类中定义,在函数中声明变量 static 的作用都是一样的,重要的是变量的声明位置(类成员或函数)。
回答by young
A static variable in a function is initialized only in the first call of that function in its running script.
函数中的静态变量仅在其运行脚本中第一次调用该函数时初始化。
回答by Gnanasekar S
At first i will explain what will happen if static variable is not used
首先我将解释如果不使用静态变量会发生什么
<?php
function somename() {
$var = 1;
echo $var . "<br />";
$var++;
}
somename();
somename();
somename();
?>
If you run the above code the output you gets will be 1 1 1 . Since everytime you called that function variable assigns to 1 and then prints it.
如果你运行上面的代码,你得到的输出将是 1 1 1 。因为每次调用该函数时,变量都会赋值为 1,然后将其打印出来。
Now lets see what if static variableis used
现在让我们看看如果使用静态变量会怎样
<?php
function somename() {
static $var = 1;
echo $var . "<br />";
$var++;
}
somename();
somename();
somename();
?>
Now if you run this code snippet the output will be 1 2 3.
现在,如果您运行此代码片段,输出将是 1 2 3。
Note:Statickeeps its value and stick around everytime the function is called. It will not lose its value when the function is called.
注意:每次调用函数时,静态都会保持其值并保持不变。当函数被调用时它不会失去它的价值。
回答by li bing zhao
class Student {
static $total_student = 0;
static function add_student(){
return Student::$total_student++;
}
}
First: for the add_student function, the best practice is to use static not public. Second: in the add_student function, we are using Student::$total_student,not use $this->total_student. This is big different from normal variable. Third:static variable are shared throughout the inheritance tree.
第一:对于 add_student 函数,最佳实践是使用 static 而不是 public。第二:在add_student函数中,我们使用的是Student::$total_student,而不是$this->total_student。这与正常变量有很大不同。第三:静态变量在整个继承树中共享。
take below code to see what is the result:
用下面的代码看看结果是什么:
class One {
static $foo ;
}
class Two extends One{}
class Three extends One{}
One::$foo = 1;
Two::$foo = 2;
Three::$foo = 3;
echo One::$foo;
echo Two::$foo;
echo Three::$foo;`