PHP final Keyword

❮ PHP Keywords

Example

Prevent inheritance of a class using the final keyword:

<?php
final class MyClass {
  public $name = "John";
}

// This code will throw an error
class AnotherClass extends MyClass{};
?>

Definition and Usage

The final keyword is used to prevent a class from being inherited and to prevent inherited method from being overridden.


Related Pages

Read more about inheritance in our PHP OOP - Inheritance Tutorial.


❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.