<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >Final Keyword</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="类与对象(PHP 5)" HREF="language.oop5.html"><LINK REL="PREVIOUS" TITLE="Magic Methods" HREF="language.oop5.magic.html"><LINK REL="NEXT" TITLE="Object cloning" HREF="language.oop5.cloning.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="language.oop5.magic.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >章 19. 类与对象(PHP 5)</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.oop5.cloning.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.oop5.final" >Final Keyword</A ></H1 ><P > PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended. </P ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6073" ></A ><P ><B >例 19-29. Final methods example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">class </font><font color="#0000BB">BaseClass </font><font color="#007700">{<br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">test</font><font color="#007700">() {<br /> echo </font><font color="#DD0000">"BaseClass::test() called\n"</font><font color="#007700">;<br /> }<br /> <br /> </font><font color="#0000BB">final public </font><font color="#007700">function </font><font color="#0000BB">moreTesting</font><font color="#007700">() {<br /> echo </font><font color="#DD0000">"BaseClass::moreTesting() called\n"</font><font color="#007700">;<br /> }<br />}<br /><br />class </font><font color="#0000BB">ChildClass </font><font color="#007700">extends </font><font color="#0000BB">BaseClass </font><font color="#007700">{<br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">moreTesting</font><font color="#007700">() {<br /> echo </font><font color="#DD0000">"ChildClass::moreTesting() called\n"</font><font color="#007700">;<br /> }<br />}<br /></font><font color="#FF8000">// Results in Fatal error: Cannot override final method BaseClass::moreTesting()<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ><TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN6076" ></A ><P ><B >例 19-30. Final class example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />final </font><font color="#007700">class </font><font color="#0000BB">BaseClass </font><font color="#007700">{<br /> </font><font color="#0000BB">public </font><font color="#007700">function </font><font color="#0000BB">test</font><font color="#007700">() {<br /> echo </font><font color="#DD0000">"BaseClass::test() called\n"</font><font color="#007700">;<br /> }<br /><br /> </font><font color="#FF8000">// Here it doesn't matter if you specify the function as final or not<br /> </font><font color="#0000BB">final public </font><font color="#007700">function </font><font color="#0000BB">moreTesting</font><font color="#007700">() {<br /> echo </font><font color="#DD0000">"BaseClass::moreTesting() called\n"</font><font color="#007700">;<br /> }<br />}<br /><br />class </font><font color="#0000BB">ChildClass </font><font color="#007700">extends </font><font color="#0000BB">BaseClass </font><font color="#007700">{<br />}<br /></font><font color="#FF8000">// Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)<br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="language.oop5.magic.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.oop5.cloning.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Magic Methods</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="language.oop5.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Object cloning</TD ></TR ></TABLE ></DIV ></BODY ></HTML >