在php web手册的侧边栏中,链接文本 addChild方法使用
::
作用域解析运算符,但示例中使用箭头运算符。有人能告诉我为什么吗?::
用于静态元素,而 ->
用于实例元素。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();
这只是表示它是对象的方法,与实际使用无关的符号。
在文档中,您不处理像$object
这样的对象实例,因此->
运算符是不正确的,因为您想要列出实际的类名。因此,遵循静态方法的用法,其中类名是静态的,您使用作用域解析运算符::
...
这通常是php文档中类的工作方式。
$obj->hello()
。但请注意,这并不表示静态方法依赖于类的实例。 - prodigitalson$this::methodName()
,你需要使用self::methodName()
或者TheClass::methodName()
。自从 5.3 版本以后,你也可以使用字符串形式的类名$classname
来调用方法,如下所示:$classname::methodName()
。 - prodigitalson