P粉1079910302023-08-30 21:49:39
You should use Foo as the return type in your example; but for fun, you can use a static return type to determine the subinstance, as shown below
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();