Skip to main content

Posts

Showing posts with the label PHP final keyword

Final Keyword in PHP

Final method: A final method is a method that cannot be overridden. To declare a method as final, you need to prefix the function name with the ‘final’ keyword. eg: class Teacher {     final public function attendance() {       echo "Teacher attendance called";    } } class Student extends Teacher {    //this will cause Compile error    public function attendance() {        echo "Student attendance called";    } } $st = new Student(); $st->attendance(); Results in Fatal error: Cannot override final method Teacher::attendance() final class: A final class is a class that cannot be extended. To declare a class as final, you need to prefix the ‘class’ keyword with ‘final’. Example below. final class Teacher {    public function attendance() {       echo "Teacher attendance called";    } } //this will cause Compile error class Studen...