ReflectionMethod_invokeArgs_basic.phpt 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. --TEST--
  2. ReflectionMethod::invokeArgs()
  3. --FILE--
  4. <?php
  5. class TestClass {
  6. public $prop = 2;
  7. public function foo() {
  8. echo "Called foo(), property = $this->prop\n";
  9. var_dump($this);
  10. return "Return Val";
  11. }
  12. public function willThrow() {
  13. throw new Exception("Called willThrow()");
  14. }
  15. public function methodWithArgs($a, $b) {
  16. echo "Called methodWithArgs($a, $b)\n";
  17. }
  18. }
  19. $testClassInstance = new TestClass();
  20. $testClassInstance->prop = "Hello";
  21. $foo = new ReflectionMethod($testClassInstance, 'foo');
  22. $methodWithArgs = new ReflectionMethod('TestClass', 'methodWithArgs');
  23. $methodThatThrows = new ReflectionMethod("TestClass::willThrow");
  24. echo "Public method:\n";
  25. var_dump($foo->invokeArgs($testClassInstance, array()));
  26. var_dump($foo->invokeArgs($testClassInstance, array(true)));
  27. echo "\nMethod with args:\n";
  28. var_dump($methodWithArgs->invokeArgs($testClassInstance, array(1, "arg2")));
  29. var_dump($methodWithArgs->invokeArgs($testClassInstance, array(1, "arg2", 3)));
  30. echo "\nMethod that throws an exception:\n";
  31. try {
  32. $methodThatThrows->invokeArgs($testClassInstance, array());
  33. } catch (Exception $e) {
  34. var_dump($e->getMessage());
  35. }
  36. ?>
  37. --EXPECTF--
  38. Public method:
  39. Called foo(), property = Hello
  40. object(TestClass)#%d (1) {
  41. ["prop"]=>
  42. string(5) "Hello"
  43. }
  44. string(10) "Return Val"
  45. Called foo(), property = Hello
  46. object(TestClass)#%d (1) {
  47. ["prop"]=>
  48. string(5) "Hello"
  49. }
  50. string(10) "Return Val"
  51. Method with args:
  52. Called methodWithArgs(1, arg2)
  53. NULL
  54. Called methodWithArgs(1, arg2)
  55. NULL
  56. Method that throws an exception:
  57. string(18) "Called willThrow()"