I have a class Child that extends my class Parent. The static function make_new_instance() creates an instance of the class. However, if I call Child::make_new_instance(), I will get a Parent returned. Can I somehow change the method Parent::make_new_instance() to make Child::make_new_instance() return a Child?
I’m not using the constructor for this, since in the actual code, there are multiple make_new_instance that all call the constructor. I don’t want override make_new_instance, since that would lead to a lot of code duplication across all subclassess and variations of make_new_instance.
class Parent {
public static function make_new_instance() {
return new Parent();
}
}
class Child extends Parent {
}
Child::make_new_instance(); // returns Parent, should return Child
If it was not calling the constructor, I’d use static:: for this.