1--TEST-- 2ReflectionMethod::invoke() 3--FILE-- 4<?php 5 6class TestClass { 7 public $prop = 2; 8 9 public function foo() { 10 echo "Called foo(), property = $this->prop\n"; 11 var_dump($this); 12 return "Return Val"; 13 } 14 15 public function willThrow() { 16 throw new Exception("Called willThrow()"); 17 } 18 19 public function methodWithArgs($a, $b) { 20 echo "Called methodWithArgs($a, $b)\n"; 21 } 22 23 public static function staticMethod() { 24 echo "Called staticMethod()\n"; 25 var_dump($this); 26 } 27 28 private static function privateMethod() { 29 echo "Called privateMethod()\n"; 30 } 31} 32 33abstract class AbstractClass { 34 abstract function foo(); 35} 36 37$foo = new ReflectionMethod('TestClass', 'foo'); 38$methodWithArgs = new ReflectionMethod('TestClass', 'methodWithArgs'); 39$staticMethod = new ReflectionMethod('TestClass::staticMethod'); 40$privateMethod = new ReflectionMethod("TestClass::privateMethod"); 41$methodThatThrows = new ReflectionMethod("TestClass::willThrow"); 42 43$testClassInstance = new TestClass(); 44$testClassInstance->prop = "Hello"; 45 46echo "Public method:\n"; 47 48var_dump($foo->invoke($testClassInstance)); 49 50var_dump($foo->invoke($testClassInstance, true)); 51 52echo "\nMethod with args:\n"; 53 54var_dump($methodWithArgs->invoke($testClassInstance, 1, "arg2")); 55var_dump($methodWithArgs->invoke($testClassInstance, 1, "arg2", 3)); 56 57echo "\nStatic method:\n"; 58 59var_dump($staticMethod->invoke()); 60var_dump($staticMethod->invoke(true)); 61var_dump($staticMethod->invoke(new stdClass())); 62 63echo "\nMethod that throws an exception:\n"; 64try { 65 var_dump($methodThatThrows->invoke($testClassInstance)); 66} catch (Exception $exc) { 67 var_dump($exc->getMessage()); 68} 69 70?> 71--EXPECTF-- 72Public method: 73Called foo(), property = Hello 74object(TestClass)#%d (1) { 75 ["prop"]=> 76 string(5) "Hello" 77} 78string(10) "Return Val" 79Called foo(), property = Hello 80object(TestClass)#%d (1) { 81 ["prop"]=> 82 string(5) "Hello" 83} 84string(10) "Return Val" 85 86Method with args: 87Called methodWithArgs(1, arg2) 88NULL 89Called methodWithArgs(1, arg2) 90NULL 91 92Static method: 93 94Warning: ReflectionMethod::invoke() expects at least 1 parameter, 0 given in %s on line %d 95NULL 96Called staticMethod() 97 98Notice: Undefined variable: this in %s on line %d 99NULL 100NULL 101Called staticMethod() 102 103Notice: Undefined variable: this in %s on line %d 104NULL 105NULL 106 107Method that throws an exception: 108string(18) "Called willThrow()" 109