这是来自 static
关键字 PHP.net 的文档:
A property declared as static cannot be accessed with an instantiated class object (though a static method can).
那么为什么下面的代码有效呢?
这是他们的示例代码(我已经缩短了):
<?php
class Foo
{
public static $my_static = 'foo';
}
$foo= new Foo();
print $foo::$my_static; //print 'foo'
?>
为什么 $foo::$my_static
在这里仍然有效?谢谢大家!
最佳答案
A ::
(双冒号,或 T_PAAMAYIM_NEKUDOTAYIM
,如 PHP 解析器所称)被称为作用域解析运算符是有原因的。 它解决了对对象引用的静态属性的访问。
这在 PHP 7 之前似乎是不可能的,however an RFC was issued to address the behavior然后在语言中实现。 PHP 文档有时更新和包含新功能的速度很慢,因此会产生误导,除非您也关注 the RFC process at wiki.php.net。 .
关于php - 为什么静态属性不能被 "->"访问但可以被 "::"访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35656898/