Final journey of this life

autoload
Release: 2023-04-09 19:22:01
Original
1978 people have browsed it

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{}//最终类
?>
Copy after login

2. The final class cannot be inherited

<?php

final class Man{}//最终类

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

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(){}		//致命错误:不能重写父类中的最终方法
}
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!