1.final关键字(不能修饰属性,即变量)

a.希望一个类不被其他类来继承(出于安全性)

案例1:

<?phpfinalclassA{}classBEXTENDSA//此用法为错误{ECHO"OK";}?>

结果:

Parse error: syntax error, unexpected T_ECHO,expecting T_FUNCTION in E:\Software_default\wamp_wwwroot\interface\interface05.phpon line 8

来自 <http://localhost/interface/interface05.php>

b.希望某个方法,不被子类改写,用final,对比案例2,案例3结果

案例2:没有重写

<?phpclassA{publicfunctiongetrate($salary){return$salary;}}classBextendsA{}$b=newB();echo$b->getrate(100);?>

结果:

100

来自 <http://localhost/interface/interface06.php>

案例3:覆盖

<?phpclassA{publicfunctiongetrate($salary){return$salary;}}classBextendsA//{publicfunctiongetrate($salary){return$salary*100;}}$b=newB();echo$b->getrate(100);?>

结果:

10000

来自 <http://localhost/interface/interface07.php>

案例4:禁止重写

<?phpclassA{finalpublicfunctiongetrate($salary){return$salary;}}classBextendsA{publicfunctiongetrate($salary){return$salary*100;}}$b=newB();echo$b->getrate(100);?>

结果:

Fatal error: Cannot override final methodA::getrate() in E:\Software_default\wamp_wwwroot\interface\interface08.phpon line 16

来自 <http://localhost/interface/interface08.php>