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