closure_044.phpt 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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); var_dump($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'); var_dump($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. ?>
  33. --EXPECTF--
  34. Before binding
  35. bool(false)
  36. bool(false)
  37. bool(true)
  38. bool(true)
  39. After binding, null scope, no instance
  40. bool(false)
  41. bool(false)
  42. Warning: Cannot unbind $this of closure using $this in %s on line %d
  43. NULL
  44. After binding, null scope, with instance
  45. bool(false)
  46. bool(true)
  47. bool(false)
  48. bool(true)
  49. After binding, with scope, no instance
  50. bool(true)
  51. bool(false)
  52. Warning: Cannot unbind $this of closure using $this in %s on line %d
  53. NULL
  54. After binding, with scope, with instance
  55. bool(true)
  56. bool(true)
  57. bool(true)
  58. bool(true)
  59. Done.