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