您應(yīng)該在範(fàn)例中使用Foo作為傳回類型;但是為了好玩,您可以使用靜態(tài)傳回類型來確定子實(shí)例,如下所示
class Foo { /** * @return string */ public function Iam(): string { return "hello"; } } class Helper { /** * Return the class instance * * @return static */ public static function get(): static { return new self(); } } class FooBuilder extends helper { protected Foo $model; public function mememe() { echo "I am a method"; } } class FooBuilder2 extends helper { protected Foo $model; public function xray() { echo "I am a method"; } } $test = FooBuilder::get(); $test->mememe(); $test2 = FooBuilder2::get(); $test2->xray();