ReflectionMethod_invoke_basic.phpt 2.5 KB

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