1--TEST-- 2Closure 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 20class 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} 35class B extends A {} 36 37$a = new A(); 38$staticScoped = $a->getStaticClosure(); 39$nonstaticScoped = $a->getClosure(); 40 41echo "Before binding", "\n"; 42$staticUnscoped(); echo "\n"; 43$nonstaticUnscoped(); echo "\n"; 44$staticScoped(); echo "\n"; 45$nonstaticScoped(); echo "\n"; 46 47echo "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 55echo "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 61echo "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 65echo "Done.\n"; 66 67--EXPECTF-- 68Before binding 69scoped to A: bool(false) 70bound: no 71scoped to A: bool(false) 72bound: no 73scoped to A: bool(true) 74bound: no 75scoped to A: bool(true) 76bound: A 77After binding, no instance 78scoped to A: bool(false) 79bound: no 80scoped to A: bool(false) 81bound: no 82scoped to A: bool(true) 83bound: no 84scoped to A: bool(true) 85bound: no 86 87Warning: Cannot bind an instance to a static closure in %s on line %d 88After binding, with same-class instance for the bound ones 89 90Warning: Cannot bind an instance to a static closure in %s on line %d 91scoped to A: bool(false) 92bound: no 93scoped to A: bool(false) 94bound: A (should be scoped to dummy class) 95 96Warning: Cannot bind an instance to a static closure in %s on line %d 97scoped to A: bool(true) 98bound: no 99scoped to A: bool(true) 100bound: A 101After binding, with different instance for the bound ones 102scoped to A: bool(false) 103bound: B (should be scoped to dummy class) 104scoped to A: bool(true) 105bound: B 106Done.