企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## final 关键字 如果父类中的方法被声明为 `final`,则子类无法覆盖该方法。如果一个类被声明为 `final`,则不能被继承。 父类方法被声明为 `final`: ``` <?php class BaseClass { public function test() { echo "BaseClass::test() called." . PHP_EOL; } final public function moreTesting() { echo "BaseClass::moreTesting() called." . PHP_EOL; } } class ChildClass extends BaseClass { public function moreTesting() { echo "ChildClass::moreTesting() called." . PHP_EOL; } } // Results in Fatal error: Cannot override final method BaseClass::moreTesting() ?> ``` 父类被声明为 `final`: ``` <?php final class BaseClass { public function test() { echo "BaseClass::test() called." . PHP_EOL; } public public function moreTesting() { echo "BaseClass::moreTesting() called." . PHP_EOL; } } class ChildClass extends BaseClass { } // Fatal error: Class ChildClass may not inherit from final class (BaseClass) ?> ```