language/oop5/constants.xml
fc174e8d6162091550edde46159917ee7e5a2e73
...
...
@@ -1,26 +1,33 @@
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.
9
-
</para>
10
-
<para>
11
-
The value must be a constant expression, not (for example) a variable, a
12
-
property, or a function call.
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>.
13
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>
14
17
<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
+
It's also possible for interfaces to have constants. Look
19
+
at the <link linkend="language.oop5.interfaces">interface documentation</link>
20
+
for examples.
18
21
</para>
19
22
<para>
20
-
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.
21
24
The variable's value can not be a keyword (e.g. <literal>self</literal>,
22
25
<literal>parent</literal> and <literal>static</literal>).
23
26
</para>
27
+
<para>
28
+
Note that class constants are allocated once per class, and not for each
29
+
class instance.
30
+
</para>
24
31
<example>
25
32
<title>Defining and using a constant</title>
26
33
<programlisting role="php">
...
...
@@ -38,49 +45,44 @@ class MyClass
38
45
echo MyClass::CONSTANT . "\n";
39
46

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

43
50
$class = new MyClass();
44
51
$class->showConstant();
45
52

46
-
echo $class::CONSTANT."\n"; // As of PHP 5.3.0
53
+
echo $class::CONSTANT."\n";
47
54
?>
48
55
]]>
49
56
</programlisting>
50
57
</example>
58
+
<para>
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:
62
+
</para>
51
63
<example>
52
-
<title>Static data example</title>
64
+
<title>Namespaced ::class example</title>
53
65
<programlisting role="php">
54
66
<![CDATA[
55
67
<?php
56
-
class foo {
57
-
// As of PHP 5.3.0
58
-
const BAR = <<<'EOT'
59
-
bar
60
-
EOT;
61
-
// As of PHP 5.3.0
62
-
const BAZ = <<<EOT
63
-
baz
64
-
EOT;
68
+
namespace foo {
69
+
class bar {
70
+
}
71
+

72
+
echo bar::class; // foo\bar
65
73
}
66
74
?>
67
75
]]>
68
76
</programlisting>
69
77
</example>
70
-
<note>
71
-
<para>
72
-
Support for initializing constants with Heredoc and Nowdoc syntax was added in PHP 5.3.0.
73
-
</para>
74
-
</note>
78
+

75
79
<example>
76
-
<title>Constant expression example</title>
80
+
<title>Class constant expression example</title>
77
81
<programlisting role="php">
78
82
<![CDATA[
79
83
<?php
80
84
const ONE = 1;
81
-

82
85
class foo {
83
-
// As of PHP 5.6.0
84
86
const TWO = ONE * 2;
85
87
const THREE = ONE + self::TWO;
86
88
const SENTENCE = 'The value of THREE is '.self::THREE;
...
...
@@ -88,13 +90,34 @@ class foo {
88
90
?>
89
91
]]>
90
92
</programlisting>
91
-
<para>
92
-
It is possible to provide a scalar expression involving numeric and string literals and/or constants in context of a class constant.
93
-
</para>
93
+
</example>
94
+

95
+
<example>
96
+
<title>Class constant visibility modifiers, as of PHP 7.1.0</title>
97
+
<programlisting role="php">
98
+
<![CDATA[
99
+
<?php
100
+
class Foo {
101
+
public const BAR = 'bar';
102
+
private const BAZ = 'baz';
103
+
}
104
+
echo Foo::BAR, PHP_EOL;
105
+
echo Foo::BAZ, PHP_EOL;
106
+
?>
107
+
]]>
108
+
</programlisting>
109
+
&example.outputs.71;
110
+
<screen>
111
+
<![CDATA[
112
+
bar
113
+

114
+
Fatal error: Uncaught Error: Cannot access private const Foo::BAZ in …
115
+
]]>
116
+
</screen>
94
117
</example>
95
118
<note>
96
119
<para>
97
-
Constant expression support was added in PHP 5.6.0.
120
+
As of PHP 7.1.0 visibility modifiers are allowed for class constants.
98
121
</para>
99
122
</note>
100
123
</sect1>
101
124