xref: /PHP-7.4/Zend/tests/fr47160.phpt (revision a9512af8)
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$f = array('Hello','world');
40try {
41	var_dump($f('you'));
42} catch (Throwable $e) {
43	echo "Exception: " . $e->getMessage() . "\n";
44}
45try {
46	var_dump(call_user_func($f, 'you'));
47} catch (Throwable $e) {
48	echo "Exception: " . $e->getMessage() . "\n";
49}
50
51printf("-----\n");
52
53$h= new Hello;
54$f = array($h,'world');
55var_dump($f('again'));
56var_dump(call_user_func($f, 'again'));
57
58printf("-----\n");
59
60function bar() {
61	return array(new Hello,'world');
62}
63$f = bar();
64var_dump($f('there'));
65var_dump(call_user_func($f, 'there'));
66
67printf("-----\n");
68
69$x = function ($c,$v) { return array($c, $v); };
70
71$c = new Hello;
72$m = 'world';
73$f = $x($c, $m);
74var_dump($f('devs'));
75var_dump(call_user_func($f, 'devs'));
76
77printf("-----\n");
78
79$f = array(new Magic, 'foo');
80$f();
81call_user_func($f);
82
83printf("-----\n");
84
85$f = array('Magic2', 'foo');
86$f();
87call_user_func($f);
88
89
90printf("-----\n");
91
92$f = array('Magic3', 'foo');
93$f();
94call_user_func($f);
95
96printf("-----\n");
97
98$f = array(new Magic3, 'foo');
99$f();
100call_user_func($f);
101
102printf("-----\n");
103
104$f = array(new Hello2, 'world');
105var_dump($f('you'));
106var_dump(call_user_func($f, 'you'));
107
108?>
109--EXPECTF--
110Deprecated: Non-static method Hello::world() should not be called statically in %s on line %d
111Hello, you
112Exception: Using $this when not in object context
113
114Deprecated: %son-static method Hello::world() should not be called statically in %s on line %d
115Hello, you
116Exception: Using $this when not in object context
117-----
118Hello, again
119object(Hello)#%d (0) {
120}
121Hello, again
122object(Hello)#%d (0) {
123}
124-----
125Hello, there
126object(Hello)#%d (0) {
127}
128Hello, there
129object(Hello)#%d (0) {
130}
131-----
132Hello, devs
133object(Hello)#%d (0) {
134}
135Hello, devs
136object(Hello)#%d (0) {
137}
138-----
139Magic::__call called (foo)!
140Magic::__call called (foo)!
141-----
142Magic2::__callStatic called (foo)!
143Magic2::__callStatic called (foo)!
144-----
145Magic3::__callStatic called (foo)!
146Magic3::__callStatic called (foo)!
147-----
148Magic3::__call called (foo)!
149Magic3::__call called (foo)!
150-----
151Hello, you
152NULL
153Hello, you
154NULL
155