php PHP中双冒号和箭头运算符的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3961456/
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
Difference between double colon and arrow operators in PHP?
提问by mko
回答by wildpeaks
::
is for static elements while ->
is for instance elements.
::
用于静态元素,而->
用于实例元素。
For example:
例如:
class Example {
public static function hello(){
echo 'hello';
}
public function world(){
echo 'world';
}
}
// Static method, can be called from the class name
Example::hello();
// Instance method, can only be called from an instance of the class
$obj = new Example();
$obj->world();
回答by prodigitalson
This is just notation for the fact that its the method of an object and has nothing to do with actual usage.
这只是表示它是对象的方法这一事实,与实际使用无关。
In the case of documentation youre not dealing with an instance of an object like $object
so the ->
operator wouldnt be correct since you want to list the actual class name. So following the usage for a static method where the class name is static you use the scope res. operator ::
...
在文档的情况下,您不会像$object
这样处理对象的实例,->
因为您想列出实际的类名,所以运算符是不正确的。因此,按照类名是静态的静态方法的用法,您可以使用范围 res。操作员::
...
This is generally how php documentation works for classes.
这通常是 php 文档对类的工作方式。
回答by Diod
The arrow means the addChild is called as a member of the object (in this case $sxe).
箭头表示 addChild 作为对象的成员被调用(在本例中为 $sxe)。
The double colon means that addChild is a member of the SimpleXMLElement class.
双冒号表示 addChild 是 SimpleXMLElement 类的成员。