xref: /PHP-7.4/Zend/tests/closure_046.phpt (revision 28cf0807)
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--EXPECTF--
42Before binding
43bool(false)
44bool(false)
45
46bool(true)
47bool(true)
48
49After binding, no instance
50bool(false)
51bool(false)
52
53
54Deprecated: Unbinding $this of closure is deprecated in %s on line %d
55bool(true)
56bool(false)
57
58After binding, with same-class instance for the bound one
59bool(false)
60bool(true)
61
62bool(true)
63bool(true)
64
65After binding, with different instance for the bound one
66bool(true)
67bool(true)
68
69Done.
70