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