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 (ReflectionException $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--EXPECTF-- 61invoke() on a non-object: 62 63Warning: ReflectionMethod::invoke() expects parameter 1 to be object, bool given in %s%eReflectionMethod_invoke_error1.php on line %d 64NULL 65 66invoke() on a non-instance: 67string(72) "Given object is not an instance of the class this method was declared in" 68 69Private method: 70string(86) "Trying to invoke private method TestClass::privateMethod() from scope ReflectionMethod" 71 72Abstract method: 73string(53) "Trying to invoke abstract method AbstractClass::foo()" 74