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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:34:48  来源:igfitidea点击:

Difference between double colon and arrow operators in PHP?

php

提问by mko

In the sidebar of the php web manual, link textthe addChild method uses the ::scope resolution operator, but in the example it uses the Arrow operator. Can anyone tell me why that is?

在 php web 手册的侧边栏中,addChild 方法使用范围解析运算符的链接文本::,但在示例中它使用箭头运算符。谁能告诉我这是为什么?

回答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();

More about the static concept

更多关于静态概念

回答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 $objectso 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 类的成员。