P粉1079910302023-08-30 21:49:39
您應該在範例中使用Foo作為傳回類型;但是為了好玩,您可以使用靜態傳回類型來確定子實例,如下所示
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();