Annotation of embedaddon/php/Zend/tests/closure_041.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Closure 041: Rebinding: preservation of previous scope when not given as arg unless impossible
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: $staticUnscoped = static function () {
11: echo "scoped to A: "; var_dump(isset(A::$priv));
12: echo "bound: ", isset($this)?get_class($this):"no";
13: };
14:
15: $nonstaticUnscoped = function () {
16: echo "scoped to A: "; var_dump(isset(A::$priv));
17: echo "bound: ", isset($this)?get_class($this):"no";
18: };
19:
20: class A {
21: private static $priv = 7;
22: function getClosure() {
23: return function () {
24: echo "scoped to A: "; var_dump(isset(A::$priv));
25: echo "bound: ", isset($this)?get_class($this):"no";
26: };
27: }
28: function getStaticClosure() {
29: return static function () {
30: echo "scoped to A: "; var_dump(isset(A::$priv));
31: echo "bound: ", isset($this)?get_class($this):"no";
32: };
33: }
34: }
35: class B extends A {}
36:
37: $a = new A();
38: $staticScoped = $a->getStaticClosure();
39: $nonstaticScoped = $a->getClosure();
40:
41: echo "Before binding", "\n";
42: $staticUnscoped(); echo "\n";
43: $nonstaticUnscoped(); echo "\n";
44: $staticScoped(); echo "\n";
45: $nonstaticScoped(); echo "\n";
46:
47: echo "After binding, no instance", "\n";
48: $d = $staticUnscoped->bindTo(null); $d(); echo "\n";
49: $d = $nonstaticUnscoped->bindTo(null); $d(); echo "\n";
50: $d = $staticScoped->bindTo(null); $d(); echo "\n";
51: $d = $nonstaticScoped->bindTo(null); $d(); echo "\n";
52: //$d should have been turned to static
53: $d->bindTo($d);
54:
55: echo "After binding, with same-class instance for the bound ones", "\n";
56: $d = $staticUnscoped->bindTo(new A); $d(); echo "\n";
57: $d = $nonstaticUnscoped->bindTo(new A); $d(); echo " (should be scoped to dummy class)\n";
58: $d = $staticScoped->bindTo(new A); $d(); echo "\n";
59: $d = $nonstaticScoped->bindTo(new A); $d(); echo "\n";
60:
61: echo "After binding, with different instance for the bound ones", "\n";
62: $d = $nonstaticUnscoped->bindTo(new B); $d(); echo " (should be scoped to dummy class)\n";
63: $d = $nonstaticScoped->bindTo(new B); $d(); echo "\n";
64:
65: echo "Done.\n";
66:
67: --EXPECTF--
68: Before binding
69: scoped to A: bool(false)
70: bound: no
71: scoped to A: bool(false)
72: bound: no
73: scoped to A: bool(true)
74: bound: no
75: scoped to A: bool(true)
76: bound: A
77: After binding, no instance
78: scoped to A: bool(false)
79: bound: no
80: scoped to A: bool(false)
81: bound: no
82: scoped to A: bool(true)
83: bound: no
84: scoped to A: bool(true)
85: bound: no
86:
87: Warning: Cannot bind an instance to a static closure in %s on line %d
88: After binding, with same-class instance for the bound ones
89:
90: Warning: Cannot bind an instance to a static closure in %s on line %d
91: scoped to A: bool(false)
92: bound: no
93: scoped to A: bool(false)
94: bound: A (should be scoped to dummy class)
95:
96: Warning: Cannot bind an instance to a static closure in %s on line %d
97: scoped to A: bool(true)
98: bound: no
99: scoped to A: bool(true)
100: bound: A
101: After binding, with different instance for the bound ones
102: scoped to A: bool(false)
103: bound: B (should be scoped to dummy class)
104: scoped to A: bool(true)
105: bound: B
106: Done.
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>