PHP:静态和非静态函数和对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4361598/
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
PHP: Static and non Static functions and Objects
提问by Adam
What's the difference between these object callings?
这些对象调用之间有什么区别?
Non Static:
非静态:
$var = new Object;
$var->function();
Static:
静止的:
$var = User::function();
And also inside a class
why should I use the static property for functions?
还有class
为什么我应该为函数使用静态属性?
example:
例子:
static public function doSomething(){
...code...
}
回答by Mark Elliot
Static functions, by definition, cannot and do not depend on any instance properties of the class. That is, they do not require an instance of the class to execute (and so can be executed as you've shown without first creating an instance). In some sense, this means that the function doesn't (and will never need to) depend on members or methods (public or private) of the class.
根据定义,静态函数不能也不依赖于类的任何实例属性。也就是说,它们不需要类的实例来执行(因此可以像您展示的那样执行,而无需先创建实例)。从某种意义上说,这意味着该函数不(也永远不需要)依赖于该类的成员或方法(公共或私有)。
回答by Cornelius
Difference is in the variable scope. Imagine you have:
区别在于变量范围。想象一下你有:
class Student{
public $age;
static $generation = 2006;
public function readPublic(){
return $this->age;
}
public static function readStatic(){
return $this->age; // case 1
return $student1->age; // case 2
return self::$generation; // case 3
}
}
$student1 = new Student();
Student::readStatic();
You static function cannot know what is $this because it is static. If there could be a $this, it would have belonged to $student1 and not Student.
It also doesn't know what is $student1.
It does work for case 3 because it is a static variable that belongs to the class, unlike previous 2, which belong to objects that have to be instantiated.
您的静态函数无法知道 $this 是什么,因为它是静态的。如果可能存在 $this,则它属于 $student1 而不是 Student。
它也不知道 $student1 是什么。
它确实适用于情况 3,因为它是一个属于类的静态变量,与前面的 2 不同,后者属于必须实例化的对象。
回答by code_burgar
Static methods and members belong to the class itself and not to the instance of a class.
静态方法和成员属于类本身,而不属于类的实例。
回答by Robin Orheden
Static functions or fields does not rely on initialization; hence, static.
静态函数或字段不依赖于初始化;因此,静态。
回答by Vincent
Questions regarding STATIC functions keep coming back.
关于 STATIC 函数的问题不断出现。
Static functions, by definition, cannot and do not depend on any instance properties of the class. That is, they do not require an instance of the class to execute (and so can be executed. In some sense, this means that the function doesn't (and will never need to) depend on members or methods (public or private) of the class.
根据定义,静态函数不能也不依赖于类的任何实例属性。也就是说,它们不需要类的实例来执行(因此可以执行。从某种意义上说,这意味着该函数不(并且永远不需要)依赖于成员或方法(公共或私有)班级的。
class Example {
// property declaration
public $value = "The text in the property";
// method declaration
public function displayValue() {
echo $this->value;
}
static function displayText() {
echo "The text from the static function";
}
}
$instance = new Example();
$instance->displayValue();
$instance->displayText();
// Example::displayValue(); // Direct call to a non static function not allowed
Example::displayText();