closure_044.phpt 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. --TEST--
  2. Closure 044: Scope/bounding combination invariants; non static closures
  3. --FILE--
  4. <?php
  5. /* A non-static closure has a bound instance if it has a scope
  6. * and doesn't have an instance if it has no scope */
  7. $nonstaticUnscoped = function () { var_dump(isset(A::$priv)); var_dump(isset($this)); };
  8. class A {
  9. private static $priv = 7;
  10. function getClosure() {
  11. return function() { var_dump(isset(A::$priv)); var_dump(isset($this)); };
  12. }
  13. }
  14. $a = new A();
  15. $nonstaticScoped = $a->getClosure();
  16. echo "Before binding", "\n";
  17. $nonstaticUnscoped(); echo "\n";
  18. $nonstaticScoped(); echo "\n";
  19. echo "After binding, null scope, no instance", "\n";
  20. $d = $nonstaticUnscoped->bindTo(null, null); $d(); echo "\n";
  21. $d = $nonstaticScoped->bindTo(null, null); $d(); echo "\n";
  22. echo "After binding, null scope, with instance", "\n";
  23. $d = $nonstaticUnscoped->bindTo(new A, null); $d(); echo "\n";
  24. $d = $nonstaticScoped->bindTo(new A, null); $d(); echo "\n";
  25. echo "After binding, with scope, no instance", "\n";
  26. $d = $nonstaticUnscoped->bindTo(null, 'A'); $d(); echo "\n";
  27. $d = $nonstaticScoped->bindTo(null, 'A'); $d(); echo "\n";
  28. echo "After binding, with scope, with instance", "\n";
  29. $d = $nonstaticUnscoped->bindTo(new A, 'A'); $d(); echo "\n";
  30. $d = $nonstaticScoped->bindTo(new A, 'A'); $d(); echo "\n";
  31. echo "Done.\n";
  32. --EXPECT--
  33. Before binding
  34. bool(false)
  35. bool(false)
  36. bool(true)
  37. bool(true)
  38. After binding, null scope, no instance
  39. bool(false)
  40. bool(false)
  41. bool(false)
  42. bool(false)
  43. After binding, null scope, with instance
  44. bool(false)
  45. bool(true)
  46. bool(false)
  47. bool(true)
  48. After binding, with scope, no instance
  49. bool(true)
  50. bool(false)
  51. bool(true)
  52. bool(false)
  53. After binding, with scope, with instance
  54. bool(true)
  55. bool(true)
  56. bool(true)
  57. bool(true)
  58. Done.