language/oop5/final.xml
5e8652131e898cd3d02cd26eeb26da718a6aaf65
...
...
@@ -3,8 +3,8 @@
3
3
<sect1 xml:id="language.oop5.final" xmlns="http://docbook.org/ns/docbook">
4
4
<title>Final Keyword</title>
5
5
<para>
6
-
PHP 5 introduces the final keyword, which prevents child classes from
7
-
overriding a method by prefixing the definition with final. If the class
6
+
The final keyword prevents child classes from overriding a method or constant by
7
+
prefixing the definition with <literal>final</literal>. If the class
8
8
itself is being defined final then it cannot be extended.
9
9
</para>
10
10
<para>
...
...
@@ -45,7 +45,7 @@ final class BaseClass {
45
45
echo "BaseClass::test() called\n";
46
46
}
47
47

48
-
// Here it doesn't matter if you specify the function as final or not
48
+
// As the class is already final, the final keyword is redundant
49
49
final public function moreTesting() {
50
50
echo "BaseClass::moreTesting() called\n";
51
51
}
...
...
@@ -59,14 +59,38 @@ class ChildClass extends BaseClass {
59
59
</programlisting>
60
60
</example>
61
61
</para>
62
+
<para>
63
+
<example xml:id="language.oop5.final.example.php81">
64
+
<title>Final constants example as of PHP 8.1.0</title>
65
+
<programlisting role="php">
66
+
<![CDATA[
67
+
<?php
68
+
class Foo
69
+
{
70
+
final public const X = "foo";
71
+
}
72
+

73
+
class Bar extends Foo
74
+
{
75
+
public const X = "bar";
76
+
}
77
+

78
+
// Fatal error: Bar::X cannot override final constant Foo::X
79
+
?>
80
+
]]>
81
+
</programlisting>
82
+
</example>
83
+
</para>
84
+

62
85
<note>
63
86
<simpara>
64
-
Properties cannot be declared final, only classes and methods
65
-
may be declared as final.
87
+
Properties cannot be declared final: only classes, methods, and constants (as of PHP 8.1.0) may be declared as final.
88
+
</simpara>
89
+
<simpara>
90
+
As of PHP 8.0.0, private methods may not be declared final except for the constructor.
66
91
</simpara>
67
92
</note>
68
93
</sect1>
69
-
70
94
<!-- Keep this comment at the end of the file
71
95
Local variables:
72
96
mode: sgml
73
97