summaryrefslogtreecommitdiff
path: root/Zend/tests/closure_005.phpt
blob: 52b8d49059a0985ba4c1bff558e04f8f0f2514c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
--TEST--
Closure 005: Lambda inside class, lifetime of $this
--FILE--
<?php

class A {
    private $x;

    function __construct($x) {
        $this->x = $x;
    }

    function __destruct() {
        echo "Destroyed\n";
    }

    function getIncer($val) {
        return function() use ($val) {
            $this->x += $val;
        };
    }

    function getPrinter() {
        return function() {
            echo $this->x."\n";
        };
    }

    function getError() {
        return static function() {
            echo $this->x."\n";
        };
    }

    function printX() {
        echo $this->x."\n";
    }
}

$a = new A(3);
$incer = $a->getIncer(2);
$printer = $a->getPrinter();
$error = $a->getError();

$a->printX();
$printer();
$incer();
$a->printX();
$printer();

unset($a);

$incer();
$printer();

unset($incer);
$printer();

unset($printer);

$error();

echo "Done\n";
?>
--EXPECTF--
3
3
5
5
7
7
Destroyed

Fatal error: Uncaught Error: Using $this when not in object context in %sclosure_005.php:28
Stack trace:
#0 %s(%d): A::{closure}()
#1 {main}
  thrown in %sclosure_005.php on line 28