您好,欢迎访问一九零五行业门户网

php final关键字的应用

php 5 新增了一个 final 关键字。如果父类中的方法被声明为 final,则子类无法覆盖该方法。如果一个类被声明为 final,则不能被继承。
这个关键字只能用来定义类和定义方法, 不能使用final这个关键字来定义成员属性,因为final是常量的意思,我们在php里定义常量使用的是define()函数,所以不能使用final来定义成员属性。
使用final关键标记的类不能被继承;
<?phpfinal class person{ function say() { }} class student extends person{ function say() { }}?>
会出现下面错误:
fatal error: class student may not inherit from final class (person)
使用final关键标记的方法不能被子类覆盖,是最终版本;
<?phpclass person{ final function say() { } }class student extends person{ function say() { }}?>
会出现下面错误:
fatal error: cannot override final method person::say()
更多php相关知识,请访问!
以上就是php final关键字的应用的详细内容。
其它类似信息

推荐信息