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(35) "Constant [ public bool PUB ] { 1 }
61"
62getName():
63string(3) "PUB"
64getValue():
65bool(true)
66isPublic():
67bool(true)
68isPrivate():
69bool(false)
70isProtected():
71bool(false)
72isFinal():
73bool(false)
74getModifiers():
75int(1)
76getDeclaringClass():
77object(ReflectionClass)#3 (1) {
78  ["name"]=>
79  string(9) "TestClass"
80}
81getDocComment():
82string(21) "/** My Doc comment */"
83hasType():
84bool(true)
85getType():
86bool
87**********************************
88**********************************
89Reflecting on class constant TestClass::PROT
90
91__toString():
92string(38) "Constant [ protected int PROT ] { 4 }
93"
94getName():
95string(4) "PROT"
96getValue():
97int(4)
98isPublic():
99bool(false)
100isPrivate():
101bool(false)
102isProtected():
103bool(true)
104isFinal():
105bool(false)
106getModifiers():
107int(2)
108getDeclaringClass():
109object(ReflectionClass)#3 (1) {
110  ["name"]=>
111  string(9) "TestClass"
112}
113getDocComment():
114string(26) "/** Another doc comment */"
115hasType():
116bool(false)
117getType():
118NULL
119**********************************
120**********************************
121Reflecting on class constant TestClass::PRIV
122
123__toString():
124string(45) "Constant [ private string PRIV ] { keepOut }
125"
126getName():
127string(4) "PRIV"
128getValue():
129string(7) "keepOut"
130isPublic():
131bool(false)
132isPrivate():
133bool(true)
134isProtected():
135bool(false)
136isFinal():
137bool(false)
138getModifiers():
139int(4)
140getDeclaringClass():
141object(ReflectionClass)#3 (1) {
142  ["name"]=>
143  string(9) "TestClass"
144}
145getDocComment():
146bool(false)
147hasType():
148bool(false)
149getType():
150NULL
151**********************************
152**********************************
153Reflecting on class constant TestClass::FINAL
154
155__toString():
156string(47) "Constant [ final public string FINAL ] { foo }
157"
158getName():
159string(5) "FINAL"
160getValue():
161string(3) "foo"
162isPublic():
163bool(true)
164isPrivate():
165bool(false)
166isProtected():
167bool(false)
168isFinal():
169bool(true)
170getModifiers():
171int(33)
172getDeclaringClass():
173object(ReflectionClass)#3 (1) {
174  ["name"]=>
175  string(9) "TestClass"
176}
177getDocComment():
178bool(false)
179hasType():
180bool(false)
181getType():
182NULL
183**********************************
184**********************************
185Reflecting on class constant TestClass::PRIV
186
187__toString():
188string(45) "Constant [ private string PRIV ] { keepOut }
189"
190getName():
191string(4) "PRIV"
192getValue():
193string(7) "keepOut"
194isPublic():
195bool(false)
196isPrivate():
197bool(true)
198isProtected():
199bool(false)
200isFinal():
201bool(false)
202getModifiers():
203int(4)
204getDeclaringClass():
205object(ReflectionClass)#3 (1) {
206  ["name"]=>
207  string(9) "TestClass"
208}
209getDocComment():
210bool(false)
211hasType():
212bool(false)
213getType():
214NULL
215**********************************
216
217Fatal error: Uncaught ReflectionException: Constant TestClass::BAD_CONST does not exist in %s:%d
218Stack trace:
219#0 %s(%d): ReflectionClassConstant->__construct(Object(TestClass), 'BAD_CONST')
220#1 %s(%d): reflectClassConstant(Object(TestClass), 'BAD_CONST')
221#2 {main}
222  thrown in %s on line %d
223