1--TEST-- 2ReflectionMethod::invokeArgs() 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 24 25$testClassInstance = new TestClass(); 26$testClassInstance->prop = "Hello"; 27 28$foo = new ReflectionMethod($testClassInstance, 'foo'); 29$methodWithArgs = new ReflectionMethod('TestClass', 'methodWithArgs'); 30$methodThatThrows = new ReflectionMethod("TestClass::willThrow"); 31 32 33echo "Public method:\n"; 34 35var_dump($foo->invokeArgs($testClassInstance, array())); 36var_dump($foo->invokeArgs($testClassInstance, array(true))); 37 38echo "\nMethod with args:\n"; 39 40var_dump($methodWithArgs->invokeArgs($testClassInstance, array(1, "arg2"))); 41var_dump($methodWithArgs->invokeArgs($testClassInstance, array(1, "arg2", 3))); 42 43echo "\nMethod that throws an exception:\n"; 44try { 45 $methodThatThrows->invokeArgs($testClassInstance, array()); 46} catch (Exception $e) { 47 var_dump($e->getMessage()); 48} 49 50?> 51--EXPECTF-- 52Public method: 53Called foo(), property = Hello 54object(TestClass)#%d (1) { 55 ["prop"]=> 56 string(5) "Hello" 57} 58string(10) "Return Val" 59Called foo(), property = Hello 60object(TestClass)#%d (1) { 61 ["prop"]=> 62 string(5) "Hello" 63} 64string(10) "Return Val" 65 66Method with args: 67Called methodWithArgs(1, arg2) 68NULL 69Called methodWithArgs(1, arg2) 70NULL 71 72Method that throws an exception: 73string(18) "Called willThrow()" 74