PHP 5 新增了一个 final 关键字。如果父类中的方法被声明为final,则子类无法覆盖该方法; 如果一个类被声明为final,则不能被继承。
Example #1 Final 方法示例
<?php
class BaseClass {
public function test() {
echo "BaseClass::test() called
";
}
final public function moreTesting() {
echo "BaseClass::moreTesting() called
";
}
}
class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() called
";
}
}
// 产生 Fatal error: Cannot override final method BaseClass::moreTesting()
?>
Example #2 Final 类示例
<?php
final class BaseClass {
public function test() {
echo "BaseClass::test() called
";
}
// 这里无论你是否将方法声明为final,都没有关系
final public function moreTesting() {
echo "BaseClass::moreTesting() called
";
}
}
class ChildClass extends BaseClass {
}
// 产生 Fatal error: Class ChildClass may not inherit from final class (BaseClass)
?>