合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 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) ?> ```