ReflectionClassConstant_basic1.phpt 4.4 KB

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