-
Notifications
You must be signed in to change notification settings - Fork 107
/
constants.xml
137 lines (128 loc) · 3.18 KB
/
constants.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: fc174e8d6162091550edde46159917ee7e5a2e73 Maintainer: Haohappy Status: ready -->
<!-- CREDITS: Luffy -->
<sect1 xml:id="language.oop5.constants" xmlns="http://docbook.org/ns/docbook">
<title>类常量</title>
<para>
可以把在类中始终保持不变的值定义为 <link linkend="language.constants">常量</link> 。
类常量的默认可见性是 <literal>public</literal> 。
</para>
<note>
<para>
类常量可以通过子类重新定义。PHP 8.1.0 起,如果类常量定义为 <link linkend="language.oop5.final">final</link>,则不能被子类重新定义。
</para>
</note>
<para>
接口(interface)中也可以定义常量。更多示例见文档中的<link linkend="language.oop5.interfaces">接口</link>部分。
</para>
<para>
可以用一个变量来动态调用类。但该变量的值不能为关键字(如 <literal>self</literal> ,
<literal>parent</literal> 或 <literal>static</literal>)。
</para>
<para>
注意,类常量只为每个类分配一次,而不是为每个类的实例分配。
</para>
<example>
<title>定义和使用一个类常量</title>
<programlisting role="php">
<![CDATA[
<?php
class MyClass
{
const CONSTANT = 'constant value';
function showConstant() {
echo self::CONSTANT . "\n";
}
}
echo MyClass::CONSTANT . "\n";
$classname = "MyClass";
echo $classname::CONSTANT . "\n";
$class = new MyClass();
$class->showConstant();
echo $class::CONSTANT."\n";
?>
]]>
</programlisting>
</example>
<para>
特殊的 <constant>::class</constant> 允许在编译时进行完全限定的类名解析,
这在命名空间类中非常有用。
</para>
<example>
<title>命名空间的 ::class 示例</title>
<programlisting role="php">
<![CDATA[
<?php
namespace foo {
class bar {
}
echo bar::class; // foo\bar
}
?>
]]>
</programlisting>
</example>
<example>
<title>类常量表达式示例</title>
<programlisting role="php">
<![CDATA[
<?php
const ONE = 1;
class foo {
const TWO = ONE * 2;
const THREE = ONE + self::TWO;
const SENTENCE = 'The value of THREE is '.self::THREE;
}
?>
]]>
</programlisting>
</example>
<example>
<title>自 PHP 7.1.0 起,类常量支持可见性修饰符</title>
<programlisting role="php">
<![CDATA[
<?php
class Foo {
public const BAR = 'bar';
private const BAZ = 'baz';
}
echo Foo::BAR, PHP_EOL;
echo Foo::BAZ, PHP_EOL;
?>
]]>
</programlisting>
&example.outputs.71;
<screen>
<![CDATA[
bar
Fatal error: Uncaught Error: Cannot access private const Foo::BAZ in …
]]>
</screen>
</example>
<note>
<para>
自 PHP 7.1.0 起,类常量允许使用可见性修饰符。
</para>
</note>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->