【发布时间】:2013-05-01 23:05:39
【问题描述】:
由于您不能在静态函数中使用 $this->,您应该如何访问静态函数中的常规函数?
private function hey()
{
return 'hello';
}
public final static function get()
{
return $this->hey();
}
这会引发错误,因为您不能在静态变量中使用 $this->。
private function hey()
{
return 'hello';
}
public final static function get()
{
return self::hey();
}
这会引发以下错误:
Non-static method Vote::get() should not be called statically
如何在静态方法中访问常规方法?同班*
【问题讨论】:
-
如果你想调用它们,你必须将不访问
$this的函数更改为静态函数。 -
将类引用传递给您的静态方法。
-
我该怎么做?就像在 hey() 中一样,返回 $this->get("hello")?
-
对不起,但是……你真的了解静态和 oop 吗?请不要误会我的意思。
-
你不能那样做。静态方法在类范围内操作,你不能访问实例方法,因为你必须实例化这个类的一个对象(例如单例模式)