Home > Backend Development > PHP Tutorial > Analysis on usage of final keyword in php

Analysis on usage of final keyword in php

黄舟
Release: 2023-03-03 14:32:02
Original
1294 people have browsed it

The example in this article describes the usage of the final keyword in php. Share it with everyone for your reference, the details are as follows:

The final keyword can only be used to define classes and methods.

Classes marked with the final keyword cannot be inherited

final class Person{
   .......
}
class Student extends Person{
   .......
}
Copy after login

An error message will appear. Fatal error: Class Student may not inherit from final class (Person)

Methods marked with the final keyword cannot be overridden by subclasses

class Person{
   final function Say(){
     ......
   }
}
class Student extends Person{
  function Say(){
    ......
  }
}
Copy after login

The following error will appear:

Fatal Error: Cannot Override final method Person::say()

The above is the analysis of the usage of final keyword in PHP. For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


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