我在使用子类静态方法访问父级(非静态)属性时遇到问题。我已经试过这些如下:如何在静态方法中访问父非静态属性,在PHP中?
class Parent
{
protected $nonStatic;
// Other methods and properties
}
class Child extends Parent
{
public static function staticFunc()
{
$test = $this->nonStatic; # Using $this when not in object context
$test = self::$nonStatic; # Access to undeclared static property
$test = parent::$nonStatic # Access to undeclared static property
}
}
我检查了计算器类似的问题,但我没有得到任何有效的解决方案
附:对不起错别字,和上面的代码是一个虚拟的例子
代码,例如,提供,甚至没有编译。请确保您提供的代码实际上以您期望的相同方式失败。 – Charles
这里有很多错误:不是'protect $ nonStatic;'但是'protected $ nonStatic;','$ this'这个用法在静态上下文中...... –
那么如果你从实际代码中复制并粘贴了上面的代码,问题是这个'protect $ nonStatic;'它应该是'protected $ nonStatic;'仔细检查你粘贴的内容。 – PhearOfRayne