final从英文字面上很容易理解,翻译成中文就是“最终的”之意。在php面向对象编程中,final的应用主要有两个作用:
 
1、使用final修饰的类,该不能被继承
 
01 <?php
02      final class Person {
03           public $name;                                          
04           function __construct($name="" ) {
05  
06                $this->name =$name;
07                                                           
08           }
09                                                       
10           function say()  {   
11  
12                echo "我叫:". $this->name ."<br>" ;
13           }
14      }
15  
16      class student extends Person{//试图继承被final修饰的类,结果出错
17  
18      }                
19                                                                                             
20 ?>
程序运行结果:Fatal error: Class student may not inherit from final class (Person) in D:\PhpServer\wamp\www\phptest\parent.php on line 17
 
 
父类Person被final修饰后,即为最终版本,不能有子类,也不能对其进行扩展,你只能老老实实去引用它。
 
2、在类中被final修饰的成员方法,在子类中不能被覆盖
 
为防止子类扩展父类的方法可能给程序带来麻烦,同时也希望这个方法是“私有”的,是不能被扩展的,我们可以使用final关键字来修饰不需要被覆盖或者被扩展的方法。
 
01 <?php
02       class Person {
03  
04           public $name;
05                                     
06           function __construct($name="" ) {
07  
08                $this ->name =$name;                                           
09           }
10                                            
11          final function say()  {   
12                echo "我叫:". $this ->name ;
13  
14           }
15  
16      }
17  
18      class student extends Person{
19  
20        function say()  { //试图覆盖父类被final修饰的方法,结果出错   
21                                              
22       }
23      }                
24                                      
25 ?>
程序调试结果:
 
Fatal error: Cannot override final method Person::say() in D:\PhpServer\wamp\www\phptest\parent.php on line 19
 

最新文章

  1. C++中的 :: 用法
  2. 一款漂亮实用的Android开源日期控件timessquare
  3. 小白安装linux(虚拟机)red hat enterprise linux 6
  4. SQL between查询 范围查询
  5. Scala应用函数
  6. C++ 11 笔记 (二) : for循环
  7. 用 CALayer 定制下载进度条控件
  8. C# winform平台下使用spread控件导出excel表格
  9. flowable设计器插件安装
  10. 恢复oracle中误删除drop掉的表 闪回的方法
  11. popstate事件在低版本webkit中的调用
  12. MATLAB中a(:)和.&quot; &#39; &quot;,&quot; &#39; &quot;
  13. python基础1-转自金角大王
  14. CentOS 7下安装Python3.6
  15. poj-2406(kmp水题)
  16. CF 1051F
  17. java学习--第50天讲到jquery
  18. 待选框、目标框select项目左右移动
  19. jquery 之ajax,get,post异步请求简单代码模版
  20. python科学计算模块NumPy

热门文章

  1. mfc release 版本 内存不足 的解决方法
  2. MVC+EasyUI 菜单导航的实现
  3. eclipse项目debug方法
  4. 25+ Useful Selenium Web driver Code Snippets For GUI Testing Automation
  5. JavaScript 命名规则
  6. .Net中的泛型(where T : class的含义)
  7. 再看 AspriseOCR - OCR应用开发 -20151124
  8. phoenix 索引修复-基本流程
  9. 树莓派摄像头模块转成H264编码通过RTMP实现Html输出
  10. CRLF和LF