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
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短