ReflectionMethod_invoke_basic.phpt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. try {
  48. var_dump($staticMethod->invoke());
  49. } catch (TypeError $e) {
  50. echo $e->getMessage(), "\n";
  51. }
  52. try {
  53. var_dump($staticMethod->invoke(true));
  54. } catch (TypeError $e) {
  55. echo $e->getMessage(), "\n";
  56. }
  57. var_dump($staticMethod->invoke(new stdClass()));
  58. echo "\nMethod that throws an exception:\n";
  59. try {
  60. var_dump($methodThatThrows->invoke($testClassInstance));
  61. } catch (Exception $exc) {
  62. var_dump($exc->getMessage());
  63. }
  64. ?>
  65. --EXPECTF--
  66. Public method:
  67. Called foo(), property = Hello
  68. object(TestClass)#%d (1) {
  69. ["prop"]=>
  70. string(5) "Hello"
  71. }
  72. string(10) "Return Val"
  73. Called foo(), property = Hello
  74. object(TestClass)#%d (1) {
  75. ["prop"]=>
  76. string(5) "Hello"
  77. }
  78. string(10) "Return Val"
  79. Method with args:
  80. Called methodWithArgs(1, arg2)
  81. NULL
  82. Called methodWithArgs(1, arg2)
  83. NULL
  84. Static method:
  85. ReflectionMethod::invoke() expects at least 1 argument, 0 given
  86. ReflectionMethod::invoke(): Argument #1 ($object) must be of type ?object, bool given
  87. Called staticMethod()
  88. Exception: Using $this when not in object context
  89. NULL
  90. Method that throws an exception:
  91. string(18) "Called willThrow()"