1--TEST-- 2ReflectionMethod::invoke() errors 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 private static function privateMethod() { 16 echo "Called privateMethod()\n"; 17 } 18} 19 20abstract class AbstractClass { 21 abstract function foo(); 22} 23 24$foo = new ReflectionMethod('TestClass', 'foo'); 25$privateMethod = new ReflectionMethod("TestClass::privateMethod"); 26 27$testClassInstance = new TestClass(); 28$testClassInstance->prop = "Hello"; 29 30echo "invoke() on a non-object:\n"; 31try { 32 var_dump($foo->invoke(true)); 33} catch (TypeError $e) { 34 var_dump($e->getMessage()); 35} 36 37echo "\ninvoke() on a non-instance:\n"; 38try { 39 var_dump($foo->invoke(new stdClass())); 40} catch (ReflectionException $e) { 41 var_dump($e->getMessage()); 42} 43 44echo "\nPrivate method:\n"; 45try { 46 var_dump($privateMethod->invoke($testClassInstance)); 47} catch (ReflectionException $e) { 48 var_dump($e->getMessage()); 49} 50 51echo "\nAbstract method:\n"; 52$abstractMethod = new ReflectionMethod("AbstractClass::foo"); 53try { 54 $abstractMethod->invoke(true); 55} catch (ReflectionException $e) { 56 var_dump($e->getMessage()); 57} 58 59?> 60--EXPECT-- 61invoke() on a non-object: 62string(85) "ReflectionMethod::invoke(): Argument #1 ($object) must be of type ?object, bool given" 63 64invoke() on a non-instance: 65string(72) "Given object is not an instance of the class this method was declared in" 66 67Private method: 68string(86) "Trying to invoke private method TestClass::privateMethod() from scope ReflectionMethod" 69 70Abstract method: 71string(53) "Trying to invoke abstract method AbstractClass::foo()" 72