language/oop5/constants.xml
922b4b5aeb327d78ea1bb4b932e5db2e9a03ffc5
...
...
@@ -1,24 +1,26 @@
1
-
<?xml version="1.0" encoding="utf-8"?>
1
+
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
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>
...
...
@@ -26,6 +28,12 @@
26
28
Note that class constants are allocated once per class, and not for each
27
29
class instance.
28
30
</para>
31
+
<para>
32
+
As of PHP 8.3.0, class constants can have a scalar type such as <literal>bool</literal>,
33
+
<literal>int</literal>, <literal>float</literal>, <literal>string</literal>, or even
34
+
<literal>array</literal>. When using <literal>array</literal>, the contents can only be
35
+
other scalar types.
36
+
</para>
29
37
<example>
30
38
<title>Defining and using a constant</title>
31
39
<programlisting role="php">
...
...
@@ -43,43 +51,20 @@ class MyClass
43
51
echo MyClass::CONSTANT . "\n";
44
52

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

48
56
$class = new MyClass();
49
57
$class->showConstant();
50
58

51
-
echo $class::CONSTANT."\n"; // As of PHP 5.3.0
52
-
?>
53
-
]]>
54
-
</programlisting>
55
-
</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
-
}
59
+
echo $class::CONSTANT."\n";
71
60
?>
72
61
]]>
73
62
</programlisting>
74
63
</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
64
<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:
65
+
The special <constant>::class</constant> constant allows
66
+
for fully qualified class name resolution at compile time,
67
+
this is useful for namespaced classes:
83
68
</para>
84
69
<example>
85
70
<title>Namespaced ::class example</title>
...
...
@@ -96,15 +81,14 @@ namespace foo {
96
81
]]>
97
82
</programlisting>
98
83
</example>
84
+

99
85
<example>
100
-
<title>Constant expression example</title>
86
+
<title>Class constant expression example</title>
101
87
<programlisting role="php">
102
88
<![CDATA[
103
89
<?php
104
90
const ONE = 1;
105
-

106
91
class foo {
107
-
// As of PHP 5.6.0
108
92
const TWO = ONE * 2;
109
93
const THREE = ONE + self::TWO;
110
94
const SENTENCE = 'The value of THREE is '.self::THREE;
...
...
@@ -112,23 +96,14 @@ class foo {
112
96
?>
113
97
]]>
114
98
</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
99
</example>
119
-
<note>
120
-
<para>
121
-
Constant expression support was added in PHP 5.6.0.
122
-
</para>
123
-
</note>
124
100

125
101
<example>
126
-
<title>Class constant visibility modifiers</title>
102
+
<title>Class constant visibility modifiers, as of PHP 7.1.0</title>
127
103
<programlisting role="php">
128
104
<![CDATA[
129
105
<?php
130
106
class Foo {
131
-
// As of PHP 7.1.0
132
107
public const BAR = 'bar';
133
108
private const BAZ = 'baz';
134
109
}
...
...
@@ -151,6 +126,102 @@ Fatal error: Uncaught Error: Cannot access private const Foo::BAZ in …
151
126
As of PHP 7.1.0 visibility modifiers are allowed for class constants.
152
127
</para>
153
128
</note>
129
+
<example>
130
+
<title>Class constant visibility variance check, as of PHP 8.3.0</title>
131
+
<programlisting role="php">
132
+
<![CDATA[
133
+
<?php
134
+

135
+
interface MyInterface
136
+
{
137
+
public const VALUE = 42;
138
+
}
139
+

140
+
class MyClass implements MyInterface
141
+
{
142
+
protected const VALUE = 42;
143
+
}
144
+
?>
145
+
]]>
146
+
</programlisting>
147
+
&example.outputs.83;
148
+
<screen>
149
+
<![CDATA[
150
+
Fatal error: Access level to MyClass::VALUE must be public (as in interface MyInterface) …
151
+
]]>
152
+
</screen>
153
+
</example>
154
+
<note>
155
+
<simpara>
156
+
As of PHP 8.3.0 visibility variance is checked more strictly.
157
+
Prior to this version, the visibility of a class constant could be different
158
+
from the visibility of the constant in the implemented interface.
159
+
</simpara>
160
+
</note>
161
+
<example>
162
+
<title>Fetch class constant syntax, as of PHP 8.3.0</title>
163
+
<programlisting role="php">
164
+
<![CDATA[
165
+
<?php
166
+
class Foo {
167
+
public const BAR = 'bar';
168
+
private const BAZ = 'baz';
169
+
}
170
+

171
+
$name = 'BAR';
172
+
echo Foo::{$name}, PHP_EOL; // bar
173
+
?>
174
+
]]>
175
+
</programlisting>
176
+
</example>
177
+
<note>
178
+
<para>
179
+
As of PHP 8.3.0, class constants can be fetched dynamically using a
180
+
variable.
181
+
</para>
182
+
</note>
183
+
<example>
184
+
<title>Assigning types to class constants, as of PHP 8.3.0</title>
185
+
<programlisting role="php">
186
+
<![CDATA[
187
+
<?php
188
+

189
+
class MyClass {
190
+
public const bool MY_BOOL = true;
191
+
public const int MY_INT = 1;
192
+
public const float MY_FLOAT = 1.01;
193
+
public const string MY_STRING = 'one';
194
+
public const array MY_ARRAY = [self::MY_BOOL, self::MY_INT, self::MY_FLOAT, self::MY_STRING];
195
+
}
196
+

197
+
var_dump(MyClass::MY_BOOL);
198
+
var_dump(MyClass::MY_INT);
199
+
var_dump(MyClass::MY_FLOAT);
200
+
var_dump(MyClass::MY_STRING);
201
+
var_dump(MyClass::MY_ARRAY);
202
+
?>
203
+
]]>
204
+
</programlisting>
205
+
&example.outputs.83;
206
+
<screen>
207
+
<![CDATA[
208
+
bool(true)
209
+
int(1)
210
+
float(1.01)
211
+
string(3) "one"
212
+
array(4) {
213
+
[0]=>
214
+
bool(true)
215
+
[1]=>
216
+
int(1)
217
+
[2]=>
218
+
float(1.01)
219
+
[3]=>
220
+
string(3) "one"
221
+
}
222
+
]]>
223
+
</screen>
224
+
</example>
154
225
</sect1>
155
226
<!-- Keep this comment at the end of the file
156
227
Local variables:
157
228