language/oop5/constants.xml
fc174e8d6162091550edde46159917ee7e5a2e73
...
...
@@ -1,79 +1,126 @@
1
-
<?xml version="1.0" encoding="utf-8"?>
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
+
<example>
32
+
<title>Defining and using a constant</title>
33
+
<programlisting role="php">
27
34
<![CDATA[
28
35
<?php
29
36
class MyClass
30
37
{
31
-
const constant = 'constant value';
38
+
const CONSTANT = 'constant value';
32
39

33
40
function showConstant() {
34
-
echo self::constant . "\n";
41
+
echo self::CONSTANT . "\n";
35
42
}
36
43
}
37
44

38
-
echo MyClass::constant . "\n";
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";
54
+
?>
55
+
]]>
56
+
</programlisting>
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>
63
+
<example>
64
+
<title>Namespaced ::class example</title>
65
+
<programlisting role="php">
66
+
<![CDATA[
67
+
<?php
68
+
namespace foo {
69
+
class bar {
70
+
}
71
+

72
+
echo bar::class; // foo\bar
73
+
}
47
74
?>
48
75
]]>
49
-
</programlisting>
50
-
</example>
76
+
</programlisting>
77
+
</example>
51
78

52
-
<example>
53
-
<title>Static data example</title>
54
-
<programlisting role="php">
79
+
<example>
80
+
<title>Class constant expression example</title>
81
+
<programlisting role="php">
55
82
<![CDATA[
56
83
<?php
84
+
const ONE = 1;
57
85
class foo {
58
-
// As of PHP 5.3.0
59
-
const bar = <<<'EOT'
60
-
bar
61
-
EOT;
86
+
const TWO = ONE * 2;
87
+
const THREE = ONE + self::TWO;
88
+
const SENTENCE = 'The value of THREE is '.self::THREE;
89
+
}
90
+
?>
91
+
]]>
92
+
</programlisting>
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';
62
103
}
104
+
echo Foo::BAR, PHP_EOL;
105
+
echo Foo::BAZ, PHP_EOL;
63
106
?>
64
107
]]>
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>
108
+
</programlisting>
109
+
&example.outputs.71;
110
+
<screen>
111
+
<![CDATA[
112
+
bar
76
113

114
+
Fatal error: Uncaught Error: Cannot access private const Foo::BAZ in …
115
+
]]>
116
+
</screen>
117
+
</example>
118
+
<note>
119
+
<para>
120
+
As of PHP 7.1.0 visibility modifiers are allowed for class constants.
121
+
</para>
122
+
</note>
123
+
</sect1>
77
124
<!-- Keep this comment at the end of the file
78
125
Local variables:
79
126
mode: sgml
80
127