PHP5中的最终关键字
PHP5允许您停止扩展类或停止子类覆盖功能。
使用final关键字的第一种方法是在创建子类时阻止子类覆盖它们。这可用于阻止重要功能被覆盖。要在此处使用final关键字,只需将其添加到函数名称的开头即可。
class ParentClass{
final public function importantFunction() {
echo 'ParentClass::importantFunction()';
}
}
class ChildClass extends ParentClass{
public function importantFunction() {
echo 'ChildClass::importantFunction()';
}
}
$child = new ChildClass();
$child->printString();尝试覆盖此功能将产生以下错误。
Fatalerror:CannotoverridefinalmethodParentClass::importantFunction()intest.phponline12
使用final关键字的第二种方法是阻止子类的创建。如果您有一个要保留为最终版本的安全类,这将很有用。要像这样使用final关键字,只需将其附加到类名即可。
final class ParentClass{
public function importantFunction() {
echo 'ParentClass::importantFunction()';
}
}
class ChildClass extends ParentClass{
public function importantFunction() {
echo 'ChildClass::importantFunction()';
}
}
$child = new ChildClass();
$child->printString();尝试覆盖此功能将产生以下错误。
Fatalerror:ClassChildClassmaynotinheritfromfinalclass(ParentClass)intest.phponline12
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短