Respect your parent, pal
21 July 2024
It's a brief complaint on how teenage-software engineers are direly insolent nowadays.
Calling parent method is still mandatory when you override it in a child class. We don't know how parent class works and how it might change in the future. It might be initializing something essential for the whole workflow of the module. Replacing whole parent method means one is implementing template method pattern. In all other cases one has to be aware, they are violating open-close principle.
PHP even allows ignoring parent construct!
<?php
declare(strict_types=1);
class Pet extends Animal
{
public function __construct(public string $ownerName) {
}
}
class Animal
{
public function __construct(public string $name) {
}
}
$lila = new Pet('Mrs. Smith');
echo $lila->name; // Uncaught Error: Typed property must not be accessed before initialization
If overriding parent method call, ensure doing at least three things:
- Make explicit that it's overridden. i.e. for java it's @Override, for php #Override
- Make sure to call super/parent methods
- Double check the visibility. In php it's very common to bump into
protectedsuddenly becomingpublic.
Respect your parent, pal 😅