2010-03-28 22:10:10 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 06:17:58 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-06-20 22:25:43 +00:00
|
|
|
<sect1 xml:id="language.oop5.final" xmlns="http://docbook.org/ns/docbook">
|
2004-07-10 19:30:37 +00:00
|
|
|
<title>Final Keyword</title>
|
|
|
|
<para>
|
2004-07-23 18:16:04 +00:00
|
|
|
PHP 5 introduces the final keyword, which prevents child classes from
|
2013-08-16 09:25:19 +00:00
|
|
|
overriding a method by prefixing the definition with <emphasis>final</emphasis>. If the class
|
2004-09-29 19:53:43 +00:00
|
|
|
itself is being defined final then it cannot be extended.
|
2004-07-10 19:30:37 +00:00
|
|
|
</para>
|
2009-04-09 11:03:38 +00:00
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Final methods example</title>
|
2004-07-23 18:16:04 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
class BaseClass {
|
|
|
|
public function test() {
|
|
|
|
echo "BaseClass::test() called\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
final public function moreTesting() {
|
|
|
|
echo "BaseClass::moreTesting() called\n";
|
|
|
|
}
|
|
|
|
}
|
2004-07-10 19:30:37 +00:00
|
|
|
|
2004-07-23 18:16:04 +00:00
|
|
|
class ChildClass extends BaseClass {
|
|
|
|
public function moreTesting() {
|
|
|
|
echo "ChildClass::moreTesting() called\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Results in Fatal error: Cannot override final method BaseClass::moreTesting()
|
|
|
|
?>
|
|
|
|
]]>
|
|
|
|
</programlisting>
|
|
|
|
</example>
|
2009-04-09 11:03:38 +00:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<example>
|
|
|
|
<title>Final class example</title>
|
2004-09-29 19:53:43 +00:00
|
|
|
<programlisting role="php">
|
|
|
|
<![CDATA[
|
|
|
|
<?php
|
|
|
|
final class BaseClass {
|
|
|
|
public function test() {
|
|
|
|
echo "BaseClass::test() called\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here it doesn't matter if you specify the function as final or not
|
|
|
|
final public function moreTesting() {
|
|
|
|
echo "BaseClass::moreTesting() called\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ChildClass extends BaseClass {
|
|
|
|
}
|
|
|
|
// Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)
|
|
|
|
?>
|
|
|
|
]]>
|
2009-04-09 11:03:38 +00:00
|
|
|
</programlisting>
|
|
|
|
</example>
|
|
|
|
</para>
|
|
|
|
<note>
|
|
|
|
<simpara>
|
|
|
|
Properties cannot be declared final, only classes and methods
|
|
|
|
may be declared as final.
|
|
|
|
</simpara>
|
|
|
|
</note>
|
2004-07-10 19:30:37 +00:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
|
|
Local variables:
|
|
|
|
mode: sgml
|
|
|
|
sgml-omittag:t
|
|
|
|
sgml-shorttag:t
|
|
|
|
sgml-minimize-attributes:nil
|
|
|
|
sgml-always-quote-attributes:t
|
|
|
|
sgml-indent-step:1
|
|
|
|
sgml-indent-data:t
|
|
|
|
indent-tabs-mode:nil
|
|
|
|
sgml-parent-document:nil
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2004-07-10 19:30:37 +00:00
|
|
|
sgml-exposed-tags:nil
|
|
|
|
sgml-local-catalogs:nil
|
|
|
|
sgml-local-ecat-files:nil
|
|
|
|
End:
|
|
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
|
|
vim: et tw=78 syn=sgml
|
|
|
|
vi: ts=1 sw=1
|
|
|
|
-->
|