language/oop5/final.xml
907f8ab64150c8503cb9f33d274ab6a7211b86a3
907f8ab64150c8503cb9f33d274ab6a7211b86a3
...
...
@@ -1,16 +1,16 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-
<sect1 xml:id="language.oop5.final" xmlns="http://docbook.org/ns/docbook">
4
-
<title>Final Keyword</title>
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
8
-
itself is being defined final then it cannot be extended.
9
-
</para>
10
-
<para>
11
-
<example>
12
-
<title>Final methods example</title>
13
-
<programlisting role="php">
3
+
<sect1 xml:id="language.oop5.final" xmlns="http://docbook.org/ns/docbook">
4
+
<title>Final Keyword</title>
5
+
<para>
6
+
The final keyword prevents child classes from overriding a method, property, or constant by
7
+
prefixing the definition with <literal>final</literal>. If the class
8
+
itself is being defined final then it cannot be extended.
9
+
</para>
10
+
<para>
11
+
<example>
12
+
<title>Final methods example</title>
13
+
<programlisting role="php">
14
14
<![CDATA[
15
15
<?php
16
16
class BaseClass {
...
...
@@ -33,11 +33,11 @@ class ChildClass extends BaseClass {
33
33
]]>
34
34
</programlisting>
35
35
</example>
36
-
</para>
37
-
<para>
38
-
<example>
39
-
<title>Final class example</title>
40
-
<programlisting role="php">
36
+
</para>
37
+
<para>
38
+
<example>
39
+
<title>Final class example</title>
40
+
<programlisting role="php">
41
41
<![CDATA[
42
42
<?php
43
43
final class BaseClass {
...
...
@@ -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
}
...
...
@@ -56,17 +56,58 @@ class ChildClass extends BaseClass {
56
56
// Results in Fatal error: Class ChildClass may not inherit from final class (BaseClass)
57
57
?>
58
58
]]>
59
-
</programlisting>
60
-
</example>
61
-
</para>
62
-
<note>
63
-
<simpara>
64
-
Properties cannot be declared final, only classes and methods
65
-
may be declared as final.
66
-
</simpara>
67
-
</note>
68
-
</sect1>
59
+
</programlisting>
60
+
</example>
61
+
</para>
62
+
<example>
63
+
<title>Final property example as of PHP 8.4.0</title>
64
+
<programlisting role="php">
65
+
<![CDATA[
66
+
<?php
67
+
class BaseClass {
68
+
final protected string $test;
69
+
}
70
+
71
+
class ChildClass extends BaseClass {
72
+
public string $test;
73
+
}
74
+
// Results in Fatal error: Cannot override final property BaseClass::$test
75
+
?>
76
+
]]>
77
+
</programlisting>
78
+
</example>
79
+
<example xml:id="language.oop5.final.example.php81">
80
+
<title>Final constants example as of PHP 8.1.0</title>
81
+
<programlisting role="php">
82
+
<![CDATA[
83
+
<?php
84
+
class Foo
85
+
{
86
+
final public const X = "foo";
87
+
}
88
+
89
+
class Bar extends Foo
90
+
{
91
+
public const X = "bar";
92
+
}
93
+
94
+
// Fatal error: Bar::X cannot override final constant Foo::X
95
+
?>
96
+
]]>
97
+
</programlisting>
98
+
</example>
69
99
100
+
<note>
101
+
<simpara>
102
+
As of PHP 8.0.0, private methods may not be declared final except for the <link linkend="language.oop5.decon.constructor">constructor</link>.
103
+
</simpara>
104
+
</note>
105
+
<note>
106
+
<simpara>
107
+
A property that is declared <link linkend="language.oop5.visibility-members-aviz"><literal>private(set)</literal></link> is implicitly <literal>final</literal>.
108
+
</simpara>
109
+
</note>
110
+
</sect1>
70
111
<!-- Keep this comment at the end of the file
71
112
Local variables:
72
113
mode: sgml
73
114