由于您可以而不是使用$this->在静态functio中,如何访问静态函数中的常规函数?
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如何访问静态方法中的常规方法?在同一个类中*
发布于 2013-05-07 21:09:03
您可以提供对静态方法的实例的引用:
class My {
protected myProtected() {
// do something
}
public myPublic() {
// do something
}
public static myStatic(My $obj) {
$obj->myProtected(); // can access protected/private members
$obj->myPublic();
}
}
$something = new My;
// A static method call:
My::myStatic($something);
// A member function call:
$something->myPublic();如上所述,静态方法可以访问它们所属类的对象上的私有和受保护成员(属性和方法)。
或者,如果您只需要一个实例,则可以使用singleton pattern (evaluate此选项)。
https://stackoverflow.com/questions/16428457
复制相似问题