Final

  • Note

    Final class

    Prevents the class from being extended and reused

    Use when you want to protect core logic from being changed by subclassing.

    eg
    
    
                      final class Logger {
                            public function log($msg) {
                                    echo "Log: $msg";
                            }
                     }
    
    
                    // ❌ This will throw an error
                    class FileLogger extends Logger {
                    // Error: Cannot extend final class
                    }
    
                            

    Final function

    Prevents the method from being overridden in subclasses

    Use when you want to allow inheritance but lock specific methods from being changed.

    
    
                            class Animal {
                                    final public function sound() {
                                            echo "Some generic sound";
                                    }
                            }
    
                            class Dog extends Animal {
                                    // ❌ This will throw an error
                                    public function sound() {
                                            echo "Bark!";
                                    }
                            }