closure_005.phpt 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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: Uncaught Error: Using $this when not in object context in %sclosure_005.php:28
  59. Stack trace:
  60. #0 %s(%d): A::{closure}()
  61. #1 {main}
  62. thrown in %sclosure_005.php on line 28