language/oop5/constants.xml
fc174e8d6162091550edde46159917ee7e5a2e73
...
...
@@ -3,22 +3,24 @@
3
3
<sect1 xml:id="language.oop5.constants" xmlns="http://docbook.org/ns/docbook">
4
4
<title>Class Constants</title>
5
5
<para>
6
-
It is possible to define constant values on a per-class basis remaining the
7
-
same and unchangeable. Constants differ from normal variables in that you
8
-
don't use the <varname>$</varname> symbol to declare or use them.
6
+
It is possible to define <link linkend="language.constants">constants</link>
7
+
on a per-class basis remaining the same and unchangeable.
9
8
The default visibility of class constants is <literal>public</literal>.
10
9
</para>
10
+
<note>
11
+
<para>
12
+
Class constants can be redefined by a child class.
13
+
As of PHP 8.1.0, class constants cannot be redefined by a child class
14
+
if it is defined as <link linkend="language.oop5.final">final</link>.
15
+
</para>
16
+
</note>
11
17
<para>
12
-
The value must be a constant expression, not (for example) a variable, a
13
-
property, or a function call.
14
-
</para>
15
-
<para>
16
-
It's also possible for interfaces to have <literal>constants</literal>. Look at
17
-
the <link linkend="language.oop5.interfaces">interface documentation</link> for
18
-
examples.
18
+
It's also possible for interfaces to have constants. Look
19
+
at the <link linkend="language.oop5.interfaces">interface documentation</link>
20
+
for examples.
19
21
</para>
20
22
<para>
21
-
As of PHP 5.3.0, it's possible to reference the class using a variable.
23
+
It's possible to reference the class using a variable.
22
24
The variable's value can not be a keyword (e.g. <literal>self</literal>,
23
25
<literal>parent</literal> and <literal>static</literal>).
24
26
</para>
...
...
@@ -43,43 +45,20 @@ class MyClass
43
45
echo MyClass::CONSTANT . "\n";
44
46

45
47
$classname = "MyClass";
46
-
echo $classname::CONSTANT . "\n"; // As of PHP 5.3.0
48
+
echo $classname::CONSTANT . "\n";
47
49

48
50
$class = new MyClass();
49
51
$class->showConstant();
50
52

51
-
echo $class::CONSTANT."\n"; // As of PHP 5.3.0
53
+
echo $class::CONSTANT."\n";
52
54
?>
53
55
]]>
54
56
</programlisting>
55
57
</example>
56
-
<example>
57
-
<title>Static data example</title>
58
-
<programlisting role="php">
59
-
<![CDATA[
60
-
<?php
61
-
class foo {
62
-
// As of PHP 5.3.0
63
-
const BAR = <<<'EOT'
64
-
bar
65
-
EOT;
66
-
// As of PHP 5.3.0
67
-
const BAZ = <<<EOT
68
-
baz
69
-
EOT;
70
-
}
71
-
?>
72
-
]]>
73
-
</programlisting>
74
-
</example>
75
-
<note>
76
-
<para>
77
-
Support for initializing constants with Heredoc and Nowdoc syntax was added in PHP 5.3.0.
78
-
</para>
79
-
</note>
80
58
<para>
81
-
The special <constant>::class</constant> constant is available as of PHP 5.5.0, and allows
82
-
for fully qualified class name resolution at compile time, this is useful for namespaced classes:
59
+
The special <constant>::class</constant> constant allows
60
+
for fully qualified class name resolution at compile time,
61
+
this is useful for namespaced classes:
83
62
</para>
84
63
<example>
85
64
<title>Namespaced ::class example</title>
...
...
@@ -96,15 +75,14 @@ namespace foo {
96
75
]]>
97
76
</programlisting>
98
77
</example>
78
+

99
79
<example>
100
-
<title>Constant expression example</title>
80
+
<title>Class constant expression example</title>
101
81
<programlisting role="php">
102
82
<![CDATA[
103
83
<?php
104
84
const ONE = 1;
105
-

106
85
class foo {
107
-
// As of PHP 5.6.0
108
86
const TWO = ONE * 2;
109
87
const THREE = ONE + self::TWO;
110
88
const SENTENCE = 'The value of THREE is '.self::THREE;
...
...
@@ -112,23 +90,14 @@ class foo {
112
90
?>
113
91
]]>
114
92
</programlisting>
115
-
<para>
116
-
It is possible to provide a scalar expression involving numeric and string literals and/or constants in context of a class constant.
117
-
</para>
118
93
</example>
119
-
<note>
120
-
<para>
121
-
Constant expression support was added in PHP 5.6.0.
122
-
</para>
123
-
</note>
124
94

125
95
<example>
126
-
<title>Class constant visibility modifiers</title>
96
+
<title>Class constant visibility modifiers, as of PHP 7.1.0</title>
127
97
<programlisting role="php">
128
98
<![CDATA[
129
99
<?php
130
100
class Foo {
131
-
// As of PHP 7.1.0
132
101
public const BAR = 'bar';
133
102
private const BAZ = 'baz';
134
103
}
135
104