closure_041.phpt 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. --TEST--
  2. Closure 041: Rebinding: preservation of previous scope when not given as arg unless impossible
  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. $staticUnscoped = static function () {
  9. echo "scoped to A: "; var_dump(isset(A::$priv));
  10. echo "bound: ", isset($this)?get_class($this):"no";
  11. };
  12. $nonstaticUnscoped = function () {
  13. echo "scoped to A: "; var_dump(isset(A::$priv));
  14. echo "bound: ", isset($this)?get_class($this):"no";
  15. };
  16. class A {
  17. private static $priv = 7;
  18. function getClosure() {
  19. return function () {
  20. echo "scoped to A: "; var_dump(isset(A::$priv));
  21. echo "bound: ", isset($this)?get_class($this):"no";
  22. };
  23. }
  24. function getStaticClosure() {
  25. return static function () {
  26. echo "scoped to A: "; var_dump(isset(A::$priv));
  27. echo "bound: ", isset($this)?get_class($this):"no";
  28. };
  29. }
  30. }
  31. class B extends A {}
  32. $a = new A();
  33. $staticScoped = $a->getStaticClosure();
  34. $nonstaticScoped = $a->getClosure();
  35. echo "Before binding", "\n";
  36. $staticUnscoped(); echo "\n";
  37. $nonstaticUnscoped(); echo "\n";
  38. $staticScoped(); echo "\n";
  39. $nonstaticScoped(); echo "\n";
  40. echo "After binding, no instance", "\n";
  41. $d = $staticUnscoped->bindTo(null); $d(); echo "\n";
  42. $d = $nonstaticUnscoped->bindTo(null); $d(); echo "\n";
  43. $d = $staticScoped->bindTo(null); $d(); echo "\n";
  44. $d = $nonstaticScoped->bindTo(null); var_dump($d); echo "\n";
  45. echo "After binding, with same-class instance for the bound ones", "\n";
  46. $d = $staticUnscoped->bindTo(new A);
  47. $d = $nonstaticUnscoped->bindTo(new A); $d(); echo " (should be scoped to dummy class)\n";
  48. $d = $staticScoped->bindTo(new A);
  49. $d = $nonstaticScoped->bindTo(new A); $d(); echo "\n";
  50. echo "After binding, with different instance for the bound ones", "\n";
  51. $d = $nonstaticUnscoped->bindTo(new B); $d(); echo " (should be scoped to dummy class)\n";
  52. $d = $nonstaticScoped->bindTo(new B); $d(); echo "\n";
  53. echo "Done.\n";
  54. ?>
  55. --EXPECTF--
  56. Before binding
  57. scoped to A: bool(false)
  58. bound: no
  59. scoped to A: bool(false)
  60. bound: no
  61. scoped to A: bool(true)
  62. bound: no
  63. scoped to A: bool(true)
  64. bound: A
  65. After binding, no instance
  66. scoped to A: bool(false)
  67. bound: no
  68. scoped to A: bool(false)
  69. bound: no
  70. scoped to A: bool(true)
  71. bound: no
  72. Warning: Cannot unbind $this of closure using $this in %s on line %d
  73. NULL
  74. After binding, with same-class instance for the bound ones
  75. Warning: Cannot bind an instance to a static closure in %s on line %d
  76. scoped to A: bool(false)
  77. bound: A (should be scoped to dummy class)
  78. Warning: Cannot bind an instance to a static closure in %s on line %d
  79. scoped to A: bool(true)
  80. bound: A
  81. After binding, with different instance for the bound ones
  82. scoped to A: bool(false)
  83. bound: B (should be scoped to dummy class)
  84. scoped to A: bool(true)
  85. bound: B
  86. Done.