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 try { 26 var_dump($this); 27 } catch (Throwable $e) { 28 echo "Exception: " . $e->getMessage() . "\n"; 29 } 30 } 31 32 private static function privateMethod() { 33 echo "Called privateMethod()\n"; 34 } 35} 36 37abstract class AbstractClass { 38 abstract function foo(); 39} 40 41$foo = new ReflectionMethod('TestClass', 'foo'); 42$methodWithArgs = new ReflectionMethod('TestClass', 'methodWithArgs'); 43$staticMethod = new ReflectionMethod('TestClass::staticMethod'); 44$privateMethod = new ReflectionMethod("TestClass::privateMethod"); 45$methodThatThrows = new ReflectionMethod("TestClass::willThrow"); 46 47$testClassInstance = new TestClass(); 48$testClassInstance->prop = "Hello"; 49 50echo "Public method:\n"; 51 52var_dump($foo->invoke($testClassInstance)); 53 54var_dump($foo->invoke($testClassInstance, true)); 55 56echo "\nMethod with args:\n"; 57 58var_dump($methodWithArgs->invoke($testClassInstance, 1, "arg2")); 59var_dump($methodWithArgs->invoke($testClassInstance, 1, "arg2", 3)); 60 61echo "\nStatic method:\n"; 62 63var_dump($staticMethod->invoke()); 64var_dump($staticMethod->invoke(true)); 65var_dump($staticMethod->invoke(new stdClass())); 66 67echo "\nMethod that throws an exception:\n"; 68try { 69 var_dump($methodThatThrows->invoke($testClassInstance)); 70} catch (Exception $exc) { 71 var_dump($exc->getMessage()); 72} 73 74?> 75--EXPECTF-- 76Public method: 77Called foo(), property = Hello 78object(TestClass)#%d (1) { 79 ["prop"]=> 80 string(5) "Hello" 81} 82string(10) "Return Val" 83Called foo(), property = Hello 84object(TestClass)#%d (1) { 85 ["prop"]=> 86 string(5) "Hello" 87} 88string(10) "Return Val" 89 90Method with args: 91Called methodWithArgs(1, arg2) 92NULL 93Called methodWithArgs(1, arg2) 94NULL 95 96Static method: 97 98Warning: ReflectionMethod::invoke() expects at least 1 parameter, 0 given in %s on line %d 99NULL 100 101Warning: ReflectionMethod::invoke() expects parameter 1 to be object, bool given in %s on line %d 102NULL 103Called staticMethod() 104Exception: Using $this when not in object context 105NULL 106 107Method that throws an exception: 108string(18) "Called willThrow()" 109