]> granicus.if.org Git - php/blob
c3bab48d6f
[php] /
1 --TEST--
2 ReflectionMethod::invokeArgs() further errors
3 --FILE--
4 <?php
5
6 class 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     public static function staticMethod() {
16         echo "Called staticMethod()\n";
17         try {
18             var_dump($this);
19         } catch (Throwable $e) {
20             echo "Exception: " . $e->getMessage() . "\n";
21         }
22     }
23
24     private static function privateMethod() {
25         echo "Called privateMethod()\n";
26     }
27 }
28
29 abstract class AbstractClass {
30     abstract function foo();
31 }
32
33 $testClassInstance = new TestClass();
34 $testClassInstance->prop = "Hello";
35
36 $foo = new ReflectionMethod($testClassInstance, 'foo');
37 $staticMethod = new ReflectionMethod('TestClass::staticMethod');
38 $privateMethod = new ReflectionMethod("TestClass::privateMethod");
39
40 echo "\nNon-instance:\n";
41 try {
42     var_dump($foo->invokeArgs(new stdClass(), array()));
43 } catch (ReflectionException $e) {
44     var_dump($e->getMessage());
45 }
46
47 echo "\nStatic method:\n";
48
49 var_dump($staticMethod->invokeArgs(null, array()));
50
51 echo "\nPrivate method:\n";
52 try {
53     var_dump($privateMethod->invokeArgs($testClassInstance, array()));
54 } catch (ReflectionException $e) {
55     var_dump($e->getMessage());
56 }
57
58 echo "\nAbstract method:\n";
59 $abstractMethod = new ReflectionMethod("AbstractClass::foo");
60 try {
61     $abstractMethod->invokeArgs($testClassInstance, array());
62 } catch (ReflectionException $e) {
63     var_dump($e->getMessage());
64 }
65 try {
66     $abstractMethod->invokeArgs(true);
67 } catch (ReflectionException $e) {
68     var_dump($e->getMessage());
69 }
70
71 ?>
72 --EXPECT--
73 Non-instance:
74 string(72) "Given object is not an instance of the class this method was declared in"
75
76 Static method:
77 Called staticMethod()
78 Exception: Using $this when not in object context
79 NULL
80
81 Private method:
82 string(86) "Trying to invoke private method TestClass::privateMethod() from scope ReflectionMethod"
83
84 Abstract method:
85 string(53) "Trying to invoke abstract method AbstractClass::foo()"
86 string(53) "Trying to invoke abstract method AbstractClass::foo()"