Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Final keyword

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report