xref: /PHP-8.2/Zend/tests/bug54039.phpt (revision 7aacc705)
1--TEST--
2Bug #54039 (use() of static variables in lambda functions can break staticness)
3--FILE--
4<?php
5function test_1() {
6    static $v = 0;
7    ++$v;
8    echo "Outer function increments \$v to $v\n";
9    $f = function() use($v) {
10        echo "Inner function reckons \$v is $v\n";
11    };
12    return $f;
13}
14
15$f = test_1(); $f();
16$f = test_1(); $f();
17
18function test_2() {
19    static $v = 0;
20    $f = function() use($v) {
21        echo "Inner function reckons \$v is $v\n";
22    };
23    ++$v;
24    echo "Outer function increments \$v to $v\n";
25    return $f;
26}
27
28$f = test_2(); $f();
29$f = test_2(); $f();
30
31function test_3() {
32    static $v = "";
33    $v .= 'b';
34    echo "Outer function catenates 'b' onto \$v to give $v\n";
35    $f = function() use($v) {
36        echo "Inner function reckons \$v is $v\n";
37    };
38    $v .= 'a';
39    echo "Outer function catenates 'a' onto \$v to give $v\n";
40    return $f;
41}
42$f = test_3(); $f();
43$f = test_3(); $f();
44?>
45--EXPECT--
46Outer function increments $v to 1
47Inner function reckons $v is 1
48Outer function increments $v to 2
49Inner function reckons $v is 2
50Outer function increments $v to 1
51Inner function reckons $v is 0
52Outer function increments $v to 2
53Inner function reckons $v is 1
54Outer function catenates 'b' onto $v to give b
55Outer function catenates 'a' onto $v to give ba
56Inner function reckons $v is b
57Outer function catenates 'b' onto $v to give bab
58Outer function catenates 'a' onto $v to give baba
59Inner function reckons $v is bab
60