xref: /PHP-7.4/Zend/tests/arrow_functions/005.phpt (revision f3e5bbe6)
1--TEST--
2Arrow function $this binding
3--FILE--
4<?php
5
6class Test {
7    public function method() {
8        // It would be okay if this is NULL, but the rest should work
9        $fn = fn() => 42;
10        $r = new ReflectionFunction($fn);
11        var_dump($r->getClosureThis());
12
13        $fn = fn() => $this;
14        var_dump($fn());
15
16        $fn = fn() => Test::method2();
17        $fn();
18
19        $fn = fn() => call_user_func('Test::method2');
20        $fn();
21
22        $thisName = "this";
23        $fn = fn() => $$thisName;
24        var_dump($fn());
25
26        $fn = fn() => self::class;
27        var_dump($fn());
28
29        // static can be used to unbind $this
30        $fn = static fn() => isset($this);
31        var_dump($fn());
32    }
33
34    public function method2() {
35        var_dump($this);
36    }
37}
38
39(new Test)->method();
40
41?>
42--EXPECT--
43object(Test)#1 (0) {
44}
45object(Test)#1 (0) {
46}
47object(Test)#1 (0) {
48}
49object(Test)#1 (0) {
50}
51object(Test)#1 (0) {
52}
53string(4) "Test"
54bool(false)
55