ReflectionClassConstant_basic1.phpt 4.4 KB

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