Home  >  Article  >  Backend Development  >  Final journey of this life

Final journey of this life

autoload
autoloadOriginal
2021-03-04 10:51:241995browse

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: final class class name

<?php
    final class Man{}//最终类
?>

2. The final class cannot be inherited

<?php

final class Man{}//最终类

class Man2 extends Man{}		//报错:无法从final类继承
?>

3.finalThe 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn