xref: /PHP-8.1/Zend/tests/closure_call.phpt (revision 4b821f0f)
1--TEST--
2Closure::call
3--FILE--
4<?php
5
6class Foo {
7    public $x = 0;
8    function bar() {
9        return function () {
10            return $this->x;
11        };
12    }
13}
14
15$foo = new Foo;
16$qux = $foo->bar();
17
18$foobar = new Foo;
19$foobar->x = 3;
20
21var_dump($qux());
22
23var_dump($qux->call($foo));
24
25// Try on an object other than the one already bound
26var_dump($qux->call($foobar));
27
28
29$bar = function () {
30    return $this->x;
31};
32
33$elePHPant = new StdClass;
34$elePHPant->x = 7;
35
36// Try on a StdClass
37var_dump($bar->call($elePHPant));
38
39
40$beta = function ($z) {
41    return $this->x * $z;
42};
43
44// Ensure argument passing works
45var_dump($beta->call($foobar, 7));
46
47// Ensure ->call calls with scope of passed object
48class FooBar {
49    private $x = 3;
50}
51
52$foo = function () {
53    var_dump($this->x);
54};
55
56$foo->call(new FooBar);
57
58?>
59--EXPECTF--
60int(0)
61int(0)
62int(3)
63
64Warning: Cannot bind closure to scope of internal class stdClass in %s line %d
65NULL
66int(21)
67int(3)
68