xref: /PHP-7.3/Zend/tests/closure_046.phpt (revision b746e698)
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"); $d(); echo "\n";
30// $d is still non-static
31$d->bindTo($d);
32
33echo "After binding, with same-class instance for the bound one", "\n";
34$d = $nonstaticUnscoped->bindTo(new A, "static"); $d(); echo "\n";
35$d = $nonstaticScoped->bindTo(new A, "static"); $d(); echo "\n";
36
37echo "After binding, with different instance for the bound one", "\n";
38$d = $nonstaticScoped->bindTo(new B, "static"); $d(); echo "\n";
39
40echo "Done.\n";
41--EXPECT--
42Before binding
43bool(false)
44bool(false)
45
46bool(true)
47bool(true)
48
49After binding, no instance
50bool(false)
51bool(false)
52
53bool(true)
54bool(false)
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