closure_005.phpt 893 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. --TEST--
  2. Closure 005: Lambda inside class, lifetime of $this
  3. --FILE--
  4. <?php
  5. class A {
  6. private $x;
  7. function __construct($x) {
  8. $this->x = $x;
  9. }
  10. function __destruct() {
  11. echo "Destroyed\n";
  12. }
  13. function getIncer($val) {
  14. return function() use ($val) {
  15. $this->x += $val;
  16. };
  17. }
  18. function getPrinter() {
  19. return function() {
  20. echo $this->x."\n";
  21. };
  22. }
  23. function getError() {
  24. return static function() {
  25. echo $this->x."\n";
  26. };
  27. }
  28. function printX() {
  29. echo $this->x."\n";
  30. }
  31. }
  32. $a = new A(3);
  33. $incer = $a->getIncer(2);
  34. $printer = $a->getPrinter();
  35. $error = $a->getError();
  36. $a->printX();
  37. $printer();
  38. $incer();
  39. $a->printX();
  40. $printer();
  41. unset($a);
  42. $incer();
  43. $printer();
  44. unset($incer);
  45. $printer();
  46. unset($printer);
  47. $error();
  48. echo "Done\n";
  49. ?>
  50. --EXPECTF--
  51. 3
  52. 3
  53. 5
  54. 5
  55. 7
  56. 7
  57. Destroyed
  58. Fatal error: Using $this when not in object context in %sclosure_005.php on line 28