Home > Backend Development > PHP Tutorial > [php classes and objects] Final keyword

[php classes and objects] Final keyword

不言
Release: 2023-03-24 08:34:02
Original
1630 people have browsed it

This article introduces the [php classes and objects] Final keyword, which has a certain reference value. Now I share it with you. Friends in need can refer to it

Final keyword

Declared as Final:

1.类,不能被继承。
2.方法,不能被子类覆盖。
3.属性,常量,不能被定义为 Final
Copy after login
Example #1 Final 方法示例<?phpclass BaseClass {
   public function test() {
       echo "BaseClass::test() called\n";
   }   final public function moreTesting() {
       echo "BaseClass::moreTesting() called\n";
   }
}class ChildClass extends BaseClass {
   public function moreTesting() {
       echo "ChildClass::moreTesting() called\n";
   }
}// Results in Fatal error: Cannot override final method BaseClass::moreTesting()?>
Copy after login
Example #2 Final 类示例<?phpfinal class BaseClass {
   public function test() {
       echo "BaseClass::test() called\n";
   }   // 这里无论你是否将方法声明为final,都没有关系
   final public function moreTesting() {
       echo "BaseClass::moreTesting() called\n";
   }
}class ChildClass extends BaseClass {}// 产生 Fatal error: Class ChildClass may not inherit from final class (BaseClass)?>
Copy after login

Related recommendations:

[php classes and objects] late static binding

[php classes and objects] Type constraints

[php classes and objects] Object copy

The above is the detailed content of [php classes and objects] Final keyword. 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