【发布时间】:2012-12-03 16:50:08
【问题描述】:
我在子类静态方法中访问父(非静态)属性时遇到问题。我已经尝试过如下:
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
}
}
我在 stackoverflow 中检查了类似的问题,但没有得到任何可行的解决方案
P.S.对不起,错别字,上面的代码是一个虚拟示例
【问题讨论】:
-
您提供的代码甚至无法编译。请确保您提供给我们的代码实际上以您期望的方式失败。
-
这里有很多错误:不是
protect $nonStatic;,而是protected $nonStatic;,$this在静态上下文中的用法... -
好吧,如果您从实际代码中复制并粘贴了上面的代码,则您的
protect $nonStatic;存在问题,因为它应该是protected $nonStatic;,请仔细检查您粘贴的内容。 -
如果你知道什么是静态函数,你就已经知道答案了:当然不可能。为什么你忽略你的知识并尝试和错误呢?即使这样,当您的测试告诉您它不起作用时,您甚至可以在网站上创建一个问题并在顶部询问它,而无需事先分享您的知识,您确定这是不可能的,但您还是想问一下是否在 PHP 中的实现中不会有一些错误可以帮助您解决问题(无论您的问题是什么)。
标签: php static-methods non-static