xref: /PHP-8.3/Zend/tests/closure_005.phpt (revision f8d79582)
1--TEST--
2Closure 005: Lambda inside class, lifetime of $this
3--FILE--
4<?php
5
6class A {
7    private $x;
8
9    function __construct($x) {
10        $this->x = $x;
11    }
12
13    function __destruct() {
14        echo "Destroyed\n";
15    }
16
17    function getIncer($val) {
18        return function() use ($val) {
19            $this->x += $val;
20        };
21    }
22
23    function getPrinter() {
24        return function() {
25            echo $this->x."\n";
26        };
27    }
28
29    function getError() {
30        return static function() {
31            echo $this->x."\n";
32        };
33    }
34
35    function printX() {
36        echo $this->x."\n";
37    }
38}
39
40$a = new A(3);
41$incer = $a->getIncer(2);
42$printer = $a->getPrinter();
43$error = $a->getError();
44
45$a->printX();
46$printer();
47$incer();
48$a->printX();
49$printer();
50
51unset($a);
52
53$incer();
54$printer();
55
56unset($incer);
57$printer();
58
59unset($printer);
60
61$error();
62
63echo "Done\n";
64?>
65--EXPECTF--
663
673
685
695
707
717
72Destroyed
73
74Fatal error: Uncaught Error: Using $this when not in object context in %sclosure_005.php:28
75Stack trace:
76#0 %s(%d): A::{closure}()
77#1 {main}
78  thrown in %sclosure_005.php on line 28
79