language/oop5/constants.xml
922b4b5aeb327d78ea1bb4b932e5db2e9a03ffc5
...
...
@@ -1,79 +1,228 @@
1
1
<?xml version="1.0" encoding="utf-8"?>
2
2
<!-- $Revision$ -->
3
-
<sect1 xml:id="language.oop5.constants" xmlns="http://docbook.org/ns/docbook">
4
-
<title>Class Constants</title>
3
+
<sect1 xml:id="language.oop5.constants" xmlns="http://docbook.org/ns/docbook">
4
+
<title>Class Constants</title>
5
+
<para>
6
+
It is possible to define <link linkend="language.constants">constants</link>
7
+
on a per-class basis remaining the same and unchangeable.
8
+
The default visibility of class constants is <literal>public</literal>.
9
+
</para>
10
+
<note>
5
11
<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.
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>.
9
15
</para>
10
-
<para>
11
-
The value must be a constant expression, not (for example) a variable, a
12
-
property, a result of a mathematical operation, or a function call.
13
-
</para>
14
-
<para>
15
-
It's also possible for interfaces to have <literal>constants</literal>. Look at
16
-
the <link linkend="language.oop5.interfaces">interface documentation</link> for
17
-
examples.
18
-
</para>
19
-
<para>
20
-
As of PHP 5.3.0, it's possible to reference the class using a variable.
21
-
The variable's value can not be a keyword (e.g. <literal>self</literal>,
22
-
<literal>parent</literal> and <literal>static</literal>).
23
-
</para>
24
-
<example>
25
-
<title>Defining and using a constant</title>
26
-
<programlisting role="php">
16
+
</note>
17
+
<para>
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.
21
+
</para>
22
+
<para>
23
+
It's possible to reference the class using a variable.
24
+
The variable's value can not be a keyword (e.g. <literal>self</literal>,
25
+
<literal>parent</literal> and <literal>static</literal>).
26
+
</para>
27
+
<para>
28
+
Note that class constants are allocated once per class, and not for each
29
+
class instance.
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>
37
+
<example>
38
+
<title>Defining and using a constant</title>
39
+
<programlisting role="php">
27
40
<![CDATA[
28
41
<?php
29
42
class MyClass
30
43
{
31
-
const constant = 'constant value';
44
+
const CONSTANT = 'constant value';
32
45

33
46
function showConstant() {
34
-
echo self::constant . "\n";
47
+
echo self::CONSTANT . "\n";
35
48
}
36
49
}
37
50

38
-
echo MyClass::constant . "\n";
51
+
echo MyClass::CONSTANT . "\n";
39
52

40
53
$classname = "MyClass";
41
-
echo $classname::constant . "\n"; // As of PHP 5.3.0
54
+
echo $classname::CONSTANT . "\n";
42
55

43
56
$class = new MyClass();
44
57
$class->showConstant();
45
58

46
-
echo $class::constant."\n"; // As of PHP 5.3.0
59
+
echo $class::CONSTANT."\n";
60
+
?>
61
+
]]>
62
+
</programlisting>
63
+
</example>
64
+
<para>
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:
68
+
</para>
69
+
<example>
70
+
<title>Namespaced ::class example</title>
71
+
<programlisting role="php">
72
+
<![CDATA[
73
+
<?php
74
+
namespace foo {
75
+
class bar {
76
+
}
77
+

78
+
echo bar::class; // foo\bar
79
+
}
47
80
?>
48
81
]]>
49
-
</programlisting>
50
-
</example>
82
+
</programlisting>
83
+
</example>
51
84

52
-
<example>
53
-
<title>Static data example</title>
54
-
<programlisting role="php">
85
+
<example>
86
+
<title>Class constant expression example</title>
87
+
<programlisting role="php">
55
88
<![CDATA[
56
89
<?php
90
+
const ONE = 1;
57
91
class foo {
58
-
// As of PHP 5.3.0
59
-
const bar = <<<'EOT'
92
+
const TWO = ONE * 2;
93
+
const THREE = ONE + self::TWO;
94
+
const SENTENCE = 'The value of THREE is '.self::THREE;
95
+
}
96
+
?>
97
+
]]>
98
+
</programlisting>
99
+
</example>
100
+

101
+
<example>
102
+
<title>Class constant visibility modifiers, as of PHP 7.1.0</title>
103
+
<programlisting role="php">
104
+
<![CDATA[
105
+
<?php
106
+
class Foo {
107
+
public const BAR = 'bar';
108
+
private const BAZ = 'baz';
109
+
}
110
+
echo Foo::BAR, PHP_EOL;
111
+
echo Foo::BAZ, PHP_EOL;
112
+
?>
113
+
]]>
114
+
</programlisting>
115
+
&example.outputs.71;
116
+
<screen>
117
+
<![CDATA[
60
118
bar
61
-
EOT;
119
+

120
+
Fatal error: Uncaught Error: Cannot access private const Foo::BAZ in …
121
+
]]>
122
+
</screen>
123
+
</example>
124
+
<note>
125
+
<para>
126
+
As of PHP 7.1.0 visibility modifiers are allowed for class constants.
127
+
</para>
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';
62
169
}
170
+

171
+
$name = 'BAR';
172
+
echo Foo::{$name}, PHP_EOL; // bar
63
173
?>
64
174
]]>
65
-
</programlisting>
66
-
<para>
67
-
Unlike heredocs, nowdocs can be used in any static data context.
68
-
</para>
69
-
</example>
70
-
<note>
71
-
<para>
72
-
Nowdoc support was added in PHP 5.3.0.
73
-
</para>
74
-
</note>
75
-
</sect1>
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
76
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>
225
+
</sect1>
77
226
<!-- Keep this comment at the end of the file
78
227
Local variables:
79
228
mode: sgml
80
229