class Test {
public function func () {
return 'hello';
}
public static function action () {
// 如何調(diào)用 func 方法 ?
}
}
2 回答

明月笑刀無(wú)情
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
可以使用 self::func
class Test {
public function func () {
return 'hello' ;
}
public static function action () {
// 如何調(diào)用 func 方法 ?
return self::func();
}
}
但是在高版本php中已經(jīng)過時(shí)了,
Deprecated: Non-static method Test::func() should not be called statically in ……
建議使用(new self())->func();
class Test {
public function func () {
return 'hello' ;
}
public static function action () {
// 如何調(diào)用 func 方法 ?
return (new self())->func();
}
}

智慧大石
TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
靜態(tài)方法可以調(diào)用非靜態(tài)方法,使用 self 關(guān)鍵詞。php里,一個(gè)方法被self:: 后,它就自動(dòng)轉(zhuǎn)變?yōu)殪o態(tài)方法
- 2 回答
- 0 關(guān)注
- 708 瀏覽
添加回答
舉報(bào)
0/150
提交
取消