1--TEST--
2Test usage of ReflectionClassConstant methods __toString(), getName(), getValue(), isPublic(), isPrivate(), isProtected(), getModifiers(), getDeclaringClass() and getDocComment().
3--FILE--
4<?php
5
6function reflectClassConstant($base, $constant) {
7    $constInfo = new ReflectionClassConstant($base, $constant);
8    echo "**********************************\n";
9    $class = is_object($base) ? get_class($base) : $base;
10    echo "Reflecting on class constant $class::$constant\n\n";
11    echo "__toString():\n";
12    var_dump($constInfo->__toString());
13    echo "getName():\n";
14    var_dump($constInfo->getName());
15    echo "getValue():\n";
16    var_dump($constInfo->getValue());
17    echo "isPublic():\n";
18    var_dump($constInfo->isPublic());
19    echo "isPrivate():\n";
20    var_dump($constInfo->isPrivate());
21    echo "isProtected():\n";
22    var_dump($constInfo->isProtected());
23    echo "isFinal():\n";
24    var_dump($constInfo->isFinal());
25    echo "getModifiers():\n";
26    var_dump($constInfo->getModifiers());
27    echo "getDeclaringClass():\n";
28    var_dump($constInfo->getDeclaringClass());
29    echo "getDocComment():\n";
30    var_dump($constInfo->getDocComment());
31    echo "hasType():\n";
32    var_dump($constInfo->hasType());
33    echo "getType():\n";
34    echo $constInfo->getType() ?? "NULL";
35    echo "\n**********************************\n";
36}
37
38class TestClass {
39    public const bool /** My Doc comment */ PUB = true;
40    /** Another doc comment */
41    protected const PROT = 4;
42    private const PRIV = "keepOut";
43    public final const FINAL = "foo";
44}
45$instance = new TestClass();
46
47reflectClassConstant("TestClass", "PUB");
48reflectClassConstant("TestClass", "PROT");
49reflectClassConstant("TestClass", "PRIV");
50reflectClassConstant("TestClass", "FINAL");
51reflectClassConstant($instance, "PRIV");
52reflectClassConstant($instance, "BAD_CONST");
53
54?>
55--EXPECTF--
56**********************************
57Reflecting on class constant TestClass::PUB
58
59__toString():
60string(57) "/** My Doc comment */
61Constant [ public bool PUB ] { 1 }
62"
63getName():
64string(3) "PUB"
65getValue():
66bool(true)
67isPublic():
68bool(true)
69isPrivate():
70bool(false)
71isProtected():
72bool(false)
73isFinal():
74bool(false)
75getModifiers():
76int(1)
77getDeclaringClass():
78object(ReflectionClass)#3 (1) {
79  ["name"]=>
80  string(9) "TestClass"
81}
82getDocComment():
83string(21) "/** My Doc comment */"
84hasType():
85bool(true)
86getType():
87bool
88**********************************
89**********************************
90Reflecting on class constant TestClass::PROT
91
92__toString():
93string(65) "/** Another doc comment */
94Constant [ protected int PROT ] { 4 }
95"
96getName():
97string(4) "PROT"
98getValue():
99int(4)
100isPublic():
101bool(false)
102isPrivate():
103bool(false)
104isProtected():
105bool(true)
106isFinal():
107bool(false)
108getModifiers():
109int(2)
110getDeclaringClass():
111object(ReflectionClass)#3 (1) {
112  ["name"]=>
113  string(9) "TestClass"
114}
115getDocComment():
116string(26) "/** Another doc comment */"
117hasType():
118bool(false)
119getType():
120NULL
121**********************************
122**********************************
123Reflecting on class constant TestClass::PRIV
124
125__toString():
126string(45) "Constant [ private string PRIV ] { keepOut }
127"
128getName():
129string(4) "PRIV"
130getValue():
131string(7) "keepOut"
132isPublic():
133bool(false)
134isPrivate():
135bool(true)
136isProtected():
137bool(false)
138isFinal():
139bool(false)
140getModifiers():
141int(4)
142getDeclaringClass():
143object(ReflectionClass)#3 (1) {
144  ["name"]=>
145  string(9) "TestClass"
146}
147getDocComment():
148bool(false)
149hasType():
150bool(false)
151getType():
152NULL
153**********************************
154**********************************
155Reflecting on class constant TestClass::FINAL
156
157__toString():
158string(47) "Constant [ final public string FINAL ] { foo }
159"
160getName():
161string(5) "FINAL"
162getValue():
163string(3) "foo"
164isPublic():
165bool(true)
166isPrivate():
167bool(false)
168isProtected():
169bool(false)
170isFinal():
171bool(true)
172getModifiers():
173int(33)
174getDeclaringClass():
175object(ReflectionClass)#3 (1) {
176  ["name"]=>
177  string(9) "TestClass"
178}
179getDocComment():
180bool(false)
181hasType():
182bool(false)
183getType():
184NULL
185**********************************
186**********************************
187Reflecting on class constant TestClass::PRIV
188
189__toString():
190string(45) "Constant [ private string PRIV ] { keepOut }
191"
192getName():
193string(4) "PRIV"
194getValue():
195string(7) "keepOut"
196isPublic():
197bool(false)
198isPrivate():
199bool(true)
200isProtected():
201bool(false)
202isFinal():
203bool(false)
204getModifiers():
205int(4)
206getDeclaringClass():
207object(ReflectionClass)#3 (1) {
208  ["name"]=>
209  string(9) "TestClass"
210}
211getDocComment():
212bool(false)
213hasType():
214bool(false)
215getType():
216NULL
217**********************************
218
219Fatal error: Uncaught ReflectionException: Constant TestClass::BAD_CONST does not exist in %s:%d
220Stack trace:
221#0 %s(%d): ReflectionClassConstant->__construct(Object(TestClass), 'BAD_CONST')
222#1 %s(%d): reflectClassConstant(Object(TestClass), 'BAD_CONST')
223#2 {main}
224  thrown in %s on line %d
225