The Final Keyword 🚫

Published Oct 11 2023

inheritence
php
OOP

The final keyword is probably the most non-OOP and non-SOLID feature in PHP. Classes prefixed with this keyword cannot be extended. Meaning, no-hablo inheritence! This key word can be applied to class methods and constants too. Talk about being stingy, right?

Examples

Below, you can see that FordEngine cannot inherit from Engine because the final keyword is set.


final class Engine {
    public function start() {
        echo "Engine started.\n";
    }
}

class FordEngine extends Engine {
    private $engine;

    public function __construct() {
        $this->engine = new Engine();
    }

    public function start() {
        $this->engine->start();
    }
}

// Results in Fatal error: Class FordEngine may not inherit from final class (Engine)

As a SOLID lover, I call this blasphemy!

© 2025 Elvis Magagula, All rights reserved