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
|
--TEST--
Test ReflectionMethod::setAccessible().
--FILE--
<?php
class A {
private function aPrivate($a) { print __METHOD__ . "\n"; }
private static function aPrivateStatic($a) { print __METHOD__ . "\n"; }
protected function aProtected($a) { print __METHOD__ . "\n"; }
protected static function aProtectedStatic($a) { print __METHOD__ . "\n"; }
}
$private = new ReflectionMethod('A', 'aPrivate');
$privateStatic = new ReflectionMethod('A', 'aPrivateStatic');
$protected = new ReflectionMethod('A', 'aProtected');
$protectedStatic = new ReflectionMethod('A', 'aProtectedStatic');
try {
$private->invoke(new A, NULL);
}
catch (ReflectionException $e) {
var_dump($e->getMessage());
}
try {
$private->invokeArgs(new A, array(NULL));
}
catch (ReflectionException $e) {
var_dump($e->getMessage());
}
try {
$privateStatic->invoke(NULL, NULL);
}
catch (ReflectionException $e) {
var_dump($e->getMessage());
}
try {
$privateStatic->invokeArgs(NULL, array(NULL));
}
catch (ReflectionException $e) {
var_dump($e->getMessage());
}
try {
$protected->invoke(new A, NULL);
}
catch (ReflectionException $e) {
var_dump($e->getMessage());
}
try {
$protected->invokeArgs(new A, array(NULL));
}
catch (ReflectionException $e) {
var_dump($e->getMessage());
}
try {
$protectedStatic->invoke(NULL, NULL);
}
catch (ReflectionException $e) {
var_dump($e->getMessage());
}
try {
$protectedStatic->invokeArgs(NULL, array(NULL));
}
catch (ReflectionException $e) {
var_dump($e->getMessage());
}
$private->setAccessible(TRUE);
$privateStatic->setAccessible(TRUE);
$protected->setAccessible(TRUE);
$protectedStatic->setAccessible(TRUE);
$private->invoke(new A, NULL);
$private->invokeArgs(new A, array(NULL));
$privateStatic->invoke(NULL, NULL);
$privateStatic->invokeArgs(NULL, array(NULL));
$protected->invoke(new A, NULL);
$protected->invokeArgs(new A, array(NULL));
$protectedStatic->invoke(NULL, NULL);
$protectedStatic->invokeArgs(NULL, array(NULL));
?>
--EXPECT--
string(73) "Trying to invoke private method A::aPrivate() from scope ReflectionMethod"
string(73) "Trying to invoke private method A::aPrivate() from scope ReflectionMethod"
string(79) "Trying to invoke private method A::aPrivateStatic() from scope ReflectionMethod"
string(79) "Trying to invoke private method A::aPrivateStatic() from scope ReflectionMethod"
string(77) "Trying to invoke protected method A::aProtected() from scope ReflectionMethod"
string(77) "Trying to invoke protected method A::aProtected() from scope ReflectionMethod"
string(83) "Trying to invoke protected method A::aProtectedStatic() from scope ReflectionMethod"
string(83) "Trying to invoke protected method A::aProtectedStatic() from scope ReflectionMethod"
A::aPrivate
A::aPrivate
A::aPrivateStatic
A::aPrivateStatic
A::aProtected
A::aProtected
A::aProtectedStatic
A::aProtectedStatic
|