closure_041.phpt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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); $d(); echo "\n";
  45. // $d is still non-static
  46. $d->bindTo($d);
  47. echo "After binding, with same-class instance for the bound ones", "\n";
  48. $d = $staticUnscoped->bindTo(new A);
  49. $d = $nonstaticUnscoped->bindTo(new A); $d(); echo " (should be scoped to dummy class)\n";
  50. $d = $staticScoped->bindTo(new A);
  51. $d = $nonstaticScoped->bindTo(new A); $d(); echo "\n";
  52. echo "After binding, with different instance for the bound ones", "\n";
  53. $d = $nonstaticUnscoped->bindTo(new B); $d(); echo " (should be scoped to dummy class)\n";
  54. $d = $nonstaticScoped->bindTo(new B); $d(); echo "\n";
  55. echo "Done.\n";
  56. --EXPECTF--
  57. Before binding
  58. scoped to A: bool(false)
  59. bound: no
  60. scoped to A: bool(false)
  61. bound: no
  62. scoped to A: bool(true)
  63. bound: no
  64. scoped to A: bool(true)
  65. bound: A
  66. After binding, no instance
  67. scoped to A: bool(false)
  68. bound: no
  69. scoped to A: bool(false)
  70. bound: no
  71. scoped to A: bool(true)
  72. bound: no
  73. scoped to A: bool(true)
  74. bound: no
  75. After binding, with same-class instance for the bound ones
  76. Warning: Cannot bind an instance to a static closure in %s on line %d
  77. scoped to A: bool(false)
  78. bound: A (should be scoped to dummy class)
  79. Warning: Cannot bind an instance to a static closure in %s on line %d
  80. scoped to A: bool(true)
  81. bound: A
  82. After binding, with different instance for the bound ones
  83. scoped to A: bool(false)
  84. bound: B (should be scoped to dummy class)
  85. scoped to A: bool(true)
  86. bound: B
  87. Done.