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