haphpiness

These are things in PHP which make me genuinely_happy();

#[\Override] Attribute — Safe Method Overriding

PHP 8.3 added the #[\Override] attribute. When you mark a method with it, PHP guarantees that a parent class or interface actually declares that method. If the parent method is renamed or removed, you get an error immediately instead of silently having dead code.

class Base {
    protected function validate(): bool {
        return true;
    }
}

class Strict extends Base {
    #[\Override]
    protected function validate(): bool {
        // If Base::validate() is ever renamed or removed,
        // PHP throws a fatal error here. No silent breakage.
        return parent::validate() && $this->extraChecks();
    }
}

// Works with interfaces too
interface Logger {
    public function log(string $message): void;
}

class FileLogger implements Logger {
    #[\Override]
    public function log(string $message): void {
        file_put_contents('app.log', $message . PHP_EOL, FILE_APPEND);
    }
}

This is borrowed from Java's @Override and TypeScript's override keyword — both proven to prevent bugs during refactoring. It's opt-in, so you only add it where correctness matters.

Significance: Refactoring Safety

Without #[\Override], renaming a parent method silently turns the child method into dead code — a bug that no test will catch until someone notices the override isn't executing. This attribute makes refactoring across class hierarchies safe.