xref: /PHP-8.4/Zend/tests/closure_046.phpt (revision 7aacc705)
1--TEST--
2Closure 046: Rebinding: preservation of previous scope when "static" given as scope arg (same as closure #041)
3--FILE--
4<?php
5
6/* It's impossible to preserve the previous scope when doing so would break
7 * the invariants that, for non-static closures, having a scope is equivalent
8 * to having a bound instance. */
9
10$nonstaticUnscoped = function () { var_dump(isset(A::$priv)); var_dump(isset($this)); };
11
12class A {
13    private static $priv = 7;
14    function getClosure() {
15        return function() { var_dump(isset(A::$priv)); var_dump(isset($this)); };
16    }
17}
18class B extends A {}
19
20$a = new A();
21$nonstaticScoped = $a->getClosure();
22
23echo "Before binding", "\n";
24$nonstaticUnscoped(); echo "\n";
25$nonstaticScoped(); echo "\n";
26
27echo "After binding, no instance", "\n";
28$d = $nonstaticUnscoped->bindTo(null, "static"); $d(); echo "\n";
29$d = $nonstaticScoped->bindTo(null, "static"); var_dump($d); echo "\n";
30
31echo "After binding, with same-class instance for the bound one", "\n";
32$d = $nonstaticUnscoped->bindTo(new A, "static"); $d(); echo "\n";
33$d = $nonstaticScoped->bindTo(new A, "static"); $d(); echo "\n";
34
35echo "After binding, with different instance for the bound one", "\n";
36$d = $nonstaticScoped->bindTo(new B, "static"); $d(); echo "\n";
37
38echo "Done.\n";
39?>
40--EXPECTF--
41Before binding
42bool(false)
43bool(false)
44
45bool(true)
46bool(true)
47
48After binding, no instance
49bool(false)
50bool(false)
51
52
53Warning: Cannot unbind $this of closure using $this in %s on line %d
54NULL
55
56After binding, with same-class instance for the bound one
57bool(false)
58bool(true)
59
60bool(true)
61bool(true)
62
63After binding, with different instance for the bound one
64bool(true)
65bool(true)
66
67Done.
68