ReflectionClass_implementsInterface_001.phpt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. --TEST--
  2. ReflectionClass::implementsInterface()
  3. --CREDITS--
  4. Robin Fernandes <robinf@php.net>
  5. Steve Seear <stevseea@php.net>
  6. --FILE--
  7. <?php
  8. interface I1 {}
  9. class A implements I1 {}
  10. class B extends A {}
  11. interface I2 extends I1 {}
  12. class C implements I2 {}
  13. $classNames = array('A', 'B', 'C', 'I1', 'I2');
  14. foreach ($classNames as $className) {
  15. $rcs[$className] = new ReflectionClass($className);
  16. }
  17. foreach ($rcs as $childName => $child) {
  18. foreach ($rcs as $parentName => $parent) {
  19. echo "Does " . $childName . " implement " . $parentName . "?\n";
  20. echo " - Using object argument: ";
  21. try {
  22. var_dump($child->implementsInterface($parent));
  23. } catch (Exception|TypeError $e) {
  24. echo $e->getMessage() . "\n";
  25. }
  26. echo " - Using string argument: ";
  27. try {
  28. var_dump($child->implementsInterface($parentName));
  29. } catch (Exception|TypeError $e) {
  30. echo $e->getMessage() . "\n";
  31. }
  32. }
  33. }
  34. echo "\n\nTest bad arguments:\n";
  35. try {
  36. $rcs['A']->implementsInterface();
  37. } catch (ArgumentCountError $e) {
  38. echo $e->getMessage() . "\n";
  39. }
  40. try {
  41. $rcs['A']->implementsInterface('C', 'C');
  42. } catch (ArgumentCountError $e) {
  43. echo $e->getMessage() . "\n";
  44. }
  45. try {
  46. $rcs['A']->implementsInterface(null);
  47. } catch (ReflectionException $e) {
  48. echo $e->getMessage() . "\n";
  49. }
  50. try {
  51. $rcs['A']->implementsInterface('ThisClassDoesNotExist');
  52. } catch (ReflectionException $e) {
  53. echo $e->getMessage() . "\n";
  54. }
  55. try {
  56. $rcs['A']->implementsInterface(2);
  57. } catch (ReflectionException $e) {
  58. echo $e->getMessage() . "\n";
  59. }
  60. ?>
  61. --EXPECTF--
  62. Does A implement A?
  63. - Using object argument: A is not an interface
  64. - Using string argument: A is not an interface
  65. Does A implement B?
  66. - Using object argument: B is not an interface
  67. - Using string argument: B is not an interface
  68. Does A implement C?
  69. - Using object argument: C is not an interface
  70. - Using string argument: C is not an interface
  71. Does A implement I1?
  72. - Using object argument: bool(true)
  73. - Using string argument: bool(true)
  74. Does A implement I2?
  75. - Using object argument: bool(false)
  76. - Using string argument: bool(false)
  77. Does B implement A?
  78. - Using object argument: A is not an interface
  79. - Using string argument: A is not an interface
  80. Does B implement B?
  81. - Using object argument: B is not an interface
  82. - Using string argument: B is not an interface
  83. Does B implement C?
  84. - Using object argument: C is not an interface
  85. - Using string argument: C is not an interface
  86. Does B implement I1?
  87. - Using object argument: bool(true)
  88. - Using string argument: bool(true)
  89. Does B implement I2?
  90. - Using object argument: bool(false)
  91. - Using string argument: bool(false)
  92. Does C implement A?
  93. - Using object argument: A is not an interface
  94. - Using string argument: A is not an interface
  95. Does C implement B?
  96. - Using object argument: B is not an interface
  97. - Using string argument: B is not an interface
  98. Does C implement C?
  99. - Using object argument: C is not an interface
  100. - Using string argument: C is not an interface
  101. Does C implement I1?
  102. - Using object argument: bool(true)
  103. - Using string argument: bool(true)
  104. Does C implement I2?
  105. - Using object argument: bool(true)
  106. - Using string argument: bool(true)
  107. Does I1 implement A?
  108. - Using object argument: A is not an interface
  109. - Using string argument: A is not an interface
  110. Does I1 implement B?
  111. - Using object argument: B is not an interface
  112. - Using string argument: B is not an interface
  113. Does I1 implement C?
  114. - Using object argument: C is not an interface
  115. - Using string argument: C is not an interface
  116. Does I1 implement I1?
  117. - Using object argument: bool(true)
  118. - Using string argument: bool(true)
  119. Does I1 implement I2?
  120. - Using object argument: bool(false)
  121. - Using string argument: bool(false)
  122. Does I2 implement A?
  123. - Using object argument: A is not an interface
  124. - Using string argument: A is not an interface
  125. Does I2 implement B?
  126. - Using object argument: B is not an interface
  127. - Using string argument: B is not an interface
  128. Does I2 implement C?
  129. - Using object argument: C is not an interface
  130. - Using string argument: C is not an interface
  131. Does I2 implement I1?
  132. - Using object argument: bool(true)
  133. - Using string argument: bool(true)
  134. Does I2 implement I2?
  135. - Using object argument: bool(true)
  136. - Using string argument: bool(true)
  137. Test bad arguments:
  138. ReflectionClass::implementsInterface() expects exactly 1 argument, 0 given
  139. ReflectionClass::implementsInterface() expects exactly 1 argument, 2 given
  140. Deprecated: ReflectionClass::implementsInterface(): Passing null to parameter #1 ($interface) of type ReflectionClass|string is deprecated in %s on line %d
  141. Interface "" does not exist
  142. Interface "ThisClassDoesNotExist" does not exist
  143. Interface "2" does not exist