Home >Backend Development >PHP Tutorial >[php classes and objects] Final keyword
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
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()?>
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)?>
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!