xref: /PHP-8.3/Zend/tests/fr47160.phpt (revision f8d79582)
1--TEST--
2Calling method from array
3--FILE--
4<?php
5
6class Hello {
7    public function world($x) {
8        echo "Hello, $x\n";return $this;
9    }
10}
11
12class Hello2 {
13    static public function world($x) {
14        echo "Hello, $x\n";
15    }
16}
17
18class Magic {
19    public function __call($f, $a) {
20        printf("%s called (%s)!\n", __METHOD__, $f);
21    }
22}
23
24class Magic2 {
25    public static function __callStatic($f, $a) {
26        printf("%s called (%s)!\n", __METHOD__, $f);
27    }
28}
29
30class Magic3 {
31    public static function __callStatic($f, $a) {
32        printf("%s called (%s)!\n", __METHOD__, $f);
33    }
34    public function __call($f, $a) {
35        printf("%s called (%s)!\n", __METHOD__, $f);
36    }
37}
38
39$h= new Hello;
40$f = array($h,'world');
41var_dump($f('again'));
42var_dump(call_user_func($f, 'again'));
43
44printf("-----\n");
45
46function bar() {
47    return array(new Hello,'world');
48}
49$f = bar();
50var_dump($f('there'));
51var_dump(call_user_func($f, 'there'));
52
53printf("-----\n");
54
55$x = function ($c,$v) { return array($c, $v); };
56
57$c = new Hello;
58$m = 'world';
59$f = $x($c, $m);
60var_dump($f('devs'));
61var_dump(call_user_func($f, 'devs'));
62
63printf("-----\n");
64
65$f = array(new Magic, 'foo');
66$f();
67call_user_func($f);
68
69printf("-----\n");
70
71$f = array('Magic2', 'foo');
72$f();
73call_user_func($f);
74
75
76printf("-----\n");
77
78$f = array('Magic3', 'foo');
79$f();
80call_user_func($f);
81
82printf("-----\n");
83
84$f = array(new Magic3, 'foo');
85$f();
86call_user_func($f);
87
88printf("-----\n");
89
90$f = array(new Hello2, 'world');
91var_dump($f('you'));
92var_dump(call_user_func($f, 'you'));
93
94?>
95--EXPECTF--
96Hello, again
97object(Hello)#%d (0) {
98}
99Hello, again
100object(Hello)#%d (0) {
101}
102-----
103Hello, there
104object(Hello)#%d (0) {
105}
106Hello, there
107object(Hello)#%d (0) {
108}
109-----
110Hello, devs
111object(Hello)#%d (0) {
112}
113Hello, devs
114object(Hello)#%d (0) {
115}
116-----
117Magic::__call called (foo)!
118Magic::__call called (foo)!
119-----
120Magic2::__callStatic called (foo)!
121Magic2::__callStatic called (foo)!
122-----
123Magic3::__callStatic called (foo)!
124Magic3::__callStatic called (foo)!
125-----
126Magic3::__call called (foo)!
127Magic3::__call called (foo)!
128-----
129Hello, you
130NULL
131Hello, you
132NULL
133