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 "getModifiers():\n"; 24 var_dump($constInfo->getModifiers()); 25 echo "getDeclaringClass():\n"; 26 var_dump($constInfo->getDeclaringClass()); 27 echo "getDocComment():\n"; 28 var_dump($constInfo->getDocComment()); 29 echo "\n**********************************\n"; 30} 31 32class TestClass { 33 public const /** My Doc comment */ PUB = true; 34 /** Another doc comment */ 35 protected const PROT = 4; 36 private const PRIV = "keepOut"; 37} 38$instance = new TestClass(); 39 40reflectClassConstant("TestClass", "PUB"); 41reflectClassConstant("TestClass", "PROT"); 42reflectClassConstant("TestClass", "PRIV"); 43reflectClassConstant($instance, "PRIV"); 44reflectClassConstant($instance, "BAD_CONST"); 45 46?> 47--EXPECTF-- 48********************************** 49Reflecting on class constant TestClass::PUB 50 51__toString(): 52string(35) "Constant [ public bool PUB ] { 1 } 53" 54getName(): 55string(3) "PUB" 56getValue(): 57bool(true) 58isPublic(): 59bool(true) 60isPrivate(): 61bool(false) 62isProtected(): 63bool(false) 64getModifiers(): 65int(1) 66getDeclaringClass(): 67object(ReflectionClass)#3 (1) { 68 ["name"]=> 69 string(9) "TestClass" 70} 71getDocComment(): 72string(21) "/** My Doc comment */" 73 74********************************** 75********************************** 76Reflecting on class constant TestClass::PROT 77 78__toString(): 79string(38) "Constant [ protected int PROT ] { 4 } 80" 81getName(): 82string(4) "PROT" 83getValue(): 84int(4) 85isPublic(): 86bool(false) 87isPrivate(): 88bool(false) 89isProtected(): 90bool(true) 91getModifiers(): 92int(2) 93getDeclaringClass(): 94object(ReflectionClass)#3 (1) { 95 ["name"]=> 96 string(9) "TestClass" 97} 98getDocComment(): 99string(26) "/** Another doc comment */" 100 101********************************** 102********************************** 103Reflecting on class constant TestClass::PRIV 104 105__toString(): 106string(45) "Constant [ private string PRIV ] { keepOut } 107" 108getName(): 109string(4) "PRIV" 110getValue(): 111string(7) "keepOut" 112isPublic(): 113bool(false) 114isPrivate(): 115bool(true) 116isProtected(): 117bool(false) 118getModifiers(): 119int(4) 120getDeclaringClass(): 121object(ReflectionClass)#3 (1) { 122 ["name"]=> 123 string(9) "TestClass" 124} 125getDocComment(): 126bool(false) 127 128********************************** 129********************************** 130Reflecting on class constant TestClass::PRIV 131 132__toString(): 133string(45) "Constant [ private string PRIV ] { keepOut } 134" 135getName(): 136string(4) "PRIV" 137getValue(): 138string(7) "keepOut" 139isPublic(): 140bool(false) 141isPrivate(): 142bool(true) 143isProtected(): 144bool(false) 145getModifiers(): 146int(4) 147getDeclaringClass(): 148object(ReflectionClass)#3 (1) { 149 ["name"]=> 150 string(9) "TestClass" 151} 152getDocComment(): 153bool(false) 154 155********************************** 156 157Fatal error: Uncaught ReflectionException: Constant TestClass::BAD_CONST does not exist in %s:%d 158Stack trace: 159#0 %s(%d): ReflectionClassConstant->__construct(Object(TestClass), 'BAD_CONST') 160#1 %s(%d): reflectClassConstant(Object(TestClass), 'BAD_CONST') 161#2 {main} 162 thrown in %s on line %d 163