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();