Home > Article > Backend Development > Final journey of this life
First of all, the division of transactions cannot be infinitely refined, so there is definitely no need for infinite subclasses. Once subclasses appear infinitely, it may not bring convenience in solving problems, but infinite consumption of memory resources. Therefore, PHP
provides a termination mechanism so that classes cannot be inherited.
1. Basic syntax: fina
l class class name
<?php final class Man{}//最终类 ?>
2. The final class cannot be inherited
<?php final class Man{}//最终类 class Man2 extends Man{} //报错:无法从final类继承 ?>
3.final
The keyword not only modifies the class to indicate that the class cannot be inherited, but also modifies the method, indicating that the method cannot be overridden
<?php //父类 class People{ public function name(){} //普通方法 public final function age(){} //最终方法 } //子类 class Man extends People{ //重写 public function name(){} //没问题 public function age(){} //致命错误:不能重写父类中的最终方法 } ?>
Summary: The final keyword modification represents the possibility of being unchangeable in variables, represents that it cannot be inherited in classes, and cannot be overridden in methods.
Recommended: php tutorial,php video tutorial
The above is the detailed content of Final journey of this life. For more information, please follow other related articles on the PHP Chinese website!