i

PHP Tutorial

PHP-Final keyword

To avoid overriding of methods of parent class or to prevent the class inheritance, final keyword is used.

For example:

final class Doctor {
  // properties and methods
}

// will result in error as the class is final and cannot be inherited.
class Surgeon extends Doctors{
  // properties and methods
}
?>

You can also use final keyword for a method to prevent method overriding.

class Doctor {
  final public function intro() {
    // message or code
  }
}

class Surgeon extends Doctor {
  // The below code will result in error because the method intro() is final in parent class
  public function intro() {
    // message or code
  }
}
?>