language/oop5/constants.xml
fc174e8d6162091550edde46159917ee7e5a2e73
...
...
@@ -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>
...
...
@@ -43,49 +45,44 @@ 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>
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>
56
63
<example>
57
-
<title>Static data example</title>
64
+
<title>Namespaced ::class example</title>
58
65
<programlisting role="php">
59
66
<![CDATA[
60
67
<?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;
68
+
namespace foo {
69
+
class bar {
70
+
}
71
+

72
+
echo bar::class; // foo\bar
70
73
}
71
74
?>
72
75
]]>
73
76
</programlisting>
74
77
</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>
78
+

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

87
85
class foo {
88
-
// As of PHP 5.6.0
89
86
const TWO = ONE * 2;
90
87
const THREE = ONE + self::TWO;
91
88
const SENTENCE = 'The value of THREE is '.self::THREE;
...
...
@@ -93,23 +90,14 @@ class foo {
93
90
?>
94
91
]]>
95
92
</programlisting>
96
-
<para>
97
-
It is possible to provide a scalar expression involving numeric and string literals and/or constants in context of a class constant.
98
-
</para>
99
93
</example>
100
-
<note>
101
-
<para>
102
-
Constant expression support was added in PHP 5.6.0.
103
-
</para>
104
-
</note>
105
94

106
95
<example>
107
-
<title>Class constant visibility modifiers</title>
96
+
<title>Class constant visibility modifiers, as of PHP 7.1.0</title>
108
97
<programlisting role="php">
109
98
<![CDATA[
110
99
<?php
111
100
class Foo {
112
-
// As of PHP 7.1.0
113
101
public const BAR = 'bar';
114
102
private const BAZ = 'baz';
115
103
}
116
104