The return type of a method in a base class has changed. What is a good way to refactor the child method to keep its current return type?
In the following example there is the Database class, which now returns a date as a simple string. The inheriting class, on the other hand, overrides getDate and should still return a DateTime.
Note: The implementation in the base class cannot be changed.
Idea: Renaming the getDate method in the DoStuff class.
class Database
{
public function getDate(): string
{
return '01-01-0001';
}
}
class DoStuff extends Database
{
public function getDate(): DateTime
{
return DateTime::createFromFormat('d-m-Y', parent::getDate());
}
}
Are there best practices to solve such a problem? Many thanks for the help.