Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Abstract class return type of child class

How can an abstract class in PHP return the type of the child extending it?

abstract class MyAbstractClass {
    public static function fromID($id) : returnType { <======
        ...
    }
}

class MyChildClass extends MyAbstractClass {
    public function specificFunctionToThisClass(){
         ...
    }
    
}

$item = new MyChildClass()->fromID(3);
$item->specificFunctionToThisClass();

The example above is over simplified.

Right now if i call the method fromID(var) in any of the child classes they will return an object of type MyAbstractClass. How can i define the return type of that function in the MyAbstractClass so that no mather what child calls that method it will return the child's class instead of the parent one.

FYI I know i could implement the same function in every children and redirecting the call to the parent but specifying a more specific return type. Long story short my child class are more than 100 and it would be a lot easier defining it only on the parent so that any future changes to the code would not need to be done on every single child class

like image 566
nunoh123 Avatar asked Jan 26 '26 19:01

nunoh123


1 Answers

That returnType should be MyAbstractClass, without any implementation (child implements).

This might not be possible, but the child can return an instance of it's own. For dynamic configuration the $model can have configurations specific to the parent - and the child.

Make everything abstract, with abstract class and abstract static function:

abstract class Model {
    abstract static function fromArray( array $model ): Model;
    abstract static function fromID( int $id ): Model;
}

class LightSettings extends Model {
    public function __construct( array|null $config ) {
        parent::__construct( $config );
    }
    static function fromArray( array $model ): LightSettings {
        return new LightSettings( $model );
    } 
    static function fromID( int $$preset_id ): LightSettings {
        return new LightSettings( null )->load( $preset_id );
    }
    ...
}

The constructor obviously could as well take int $$preset_id, but it's flexible.

like image 155
Martin Zeitler Avatar answered Jan 29 '26 10:01

Martin Zeitler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!