closure_046.phpt 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. --TEST--
  2. Closure 046: Rebinding: preservation of previous scope when "static" given as scope arg (same as closure #041)
  3. --FILE--
  4. <?php
  5. /* It's impossible to preserve the previous scope when doing so would break
  6. * the invariants that, for non-static closures, having a scope is equivalent
  7. * to having a bound instance. */
  8. $nonstaticUnscoped = function () { var_dump(isset(A::$priv)); var_dump(isset($this)); };
  9. class A {
  10. private static $priv = 7;
  11. function getClosure() {
  12. return function() { var_dump(isset(A::$priv)); var_dump(isset($this)); };
  13. }
  14. }
  15. class B extends A {}
  16. $a = new A();
  17. $nonstaticScoped = $a->getClosure();
  18. echo "Before binding", "\n";
  19. $nonstaticUnscoped(); echo "\n";
  20. $nonstaticScoped(); echo "\n";
  21. echo "After binding, no instance", "\n";
  22. $d = $nonstaticUnscoped->bindTo(null, "static"); $d(); echo "\n";
  23. $d = $nonstaticScoped->bindTo(null, "static"); var_dump($d); echo "\n";
  24. echo "After binding, with same-class instance for the bound one", "\n";
  25. $d = $nonstaticUnscoped->bindTo(new A, "static"); $d(); echo "\n";
  26. $d = $nonstaticScoped->bindTo(new A, "static"); $d(); echo "\n";
  27. echo "After binding, with different instance for the bound one", "\n";
  28. $d = $nonstaticScoped->bindTo(new B, "static"); $d(); echo "\n";
  29. echo "Done.\n";
  30. ?>
  31. --EXPECTF--
  32. Before binding
  33. bool(false)
  34. bool(false)
  35. bool(true)
  36. bool(true)
  37. After binding, no instance
  38. bool(false)
  39. bool(false)
  40. Warning: Cannot unbind $this of closure using $this in %s on line %d
  41. NULL
  42. After binding, with same-class instance for the bound one
  43. bool(false)
  44. bool(true)
  45. bool(true)
  46. bool(true)
  47. After binding, with different instance for the bound one
  48. bool(true)
  49. bool(true)
  50. Done.