xref: /PHP-5.5/Zend/tests/closure_046.phpt (revision 38ff70ef)
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 should have been turned to 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
42--EXPECTF--
43Before binding
44bool(false)
45bool(false)
46
47bool(true)
48bool(true)
49
50After binding, no instance
51bool(false)
52bool(false)
53
54bool(true)
55bool(false)
56
57
58Warning: Cannot bind an instance to a static closure in %s on line %d
59After binding, with same-class instance for the bound one
60bool(false)
61bool(true)
62
63bool(true)
64bool(true)
65
66After binding, with different instance for the bound one
67bool(true)
68bool(true)
69
70Done.
71