【发布时间】:2010-10-18 17:01:59
【问题描述】:
在php web 手册的侧边栏中,link text addChild 方法使用:: 范围解析运算符,但在示例中它使用Arrow 运算符。谁能告诉我这是为什么?
【问题讨论】:
标签: php
在php web 手册的侧边栏中,link text addChild 方法使用:: 范围解析运算符,但在示例中它使用Arrow 运算符。谁能告诉我这是为什么?
【问题讨论】:
标签: php
:: 用于静态元素,-> 用于实例元素。
例如:
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();
【讨论】:
static 方法可以直接从实例调用,方法与调用“实例”方法相同:$obj->hello()
$obj::hello()?我的 IDE 为引用为 $this::$variableName 的变量提供了一些错误。
$this::methodName()你使用self::methodName()或TheClass::methodName()。从 5.3 开始,您还可以使用 $classname::methodName() 其中 $classname 是字符串类名。
箭头表示 addChild 被称为对象的成员(在本例中为 $sxe)。
双冒号表示 addChild 是 SimpleXMLElement 类的成员。
【讨论】:
这只是表示它是一个对象的方法,与实际使用无关。
在文档的情况下,您没有处理像 $object 这样的对象的实例,因此 -> 运算符不正确,因为您想列出实际的类名。因此,按照类名为静态的静态方法的用法,您可以使用范围 res。运营商::...
这通常是 php 文档对类的工作方式。
【讨论】: