Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Final keyword

Shulou Source: shulou.com Published: 2022-06-01 07:13:49 10月02日 Update

PHP 5 adds the final keyword. If a method in a parent class is declared final, a subclass cannot override that method. If a class is declared final, it cannot be inherited.

first embodiment

second example of the

Note: Properties cannot be defined as final, only classes and methods can be defined as final.

If we do not want a class to be inherited, we use final to decorate the class. This class cannot be inherited.

final---Used before classes and methods.

final class---cannot be inherited.

final method---cannot be overridden.

Final classes cannot be inherited.

If we do not want a class to be inherited, we use final to decorate the class. This class cannot be inherited. For example, the Math class we set involves the mathematical calculation methods we want to do. These algorithms do not need to be modified or inherited. We set it to final type.

Program Run Results

Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\test.php on line 14

final method cannot be overridden

If you don't want a method in your class to be overridden by a subclass, you can make it final by prepending it with the final modifier.

If this method is overridden by a subclass, an error will occur.

Program Run Results

Fatal error: Class SuperMath may not inherit from final class (Math) in E:\PHPProjects\test.php on line 16

Tags: Method subclass necessary program result guild this is run key keyword only attribute mathematics algorithm type error keyword Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi MariaDB Huawei vpn