closure_038.phpt 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. --TEST--
  2. Closure 038: Rebinding closures, change scope, different runtime type
  3. --FILE--
  4. <?php
  5. class A {
  6. private $x;
  7. public function __construct($v) {
  8. $this->x = $v;
  9. }
  10. public function getIncrementor() {
  11. return function() { return ++$this->x; };
  12. }
  13. }
  14. class B extends A {
  15. private $x;
  16. public function __construct($v) {
  17. parent::__construct($v);
  18. $this->x = $v*2;
  19. }
  20. }
  21. $a = new A(0);
  22. $b = new B(10);
  23. $ca = $a->getIncrementor();
  24. var_dump($ca());
  25. echo "Testing with scope given as object", "\n";
  26. $cb = $ca->bindTo($b, $b);
  27. $cb2 = Closure::bind($ca, $b, $b);
  28. var_dump($cb());
  29. var_dump($cb2());
  30. echo "Testing with scope as string", "\n";
  31. $cb = $ca->bindTo($b, 'B');
  32. $cb2 = Closure::bind($ca, $b, 'B');
  33. var_dump($cb());
  34. var_dump($cb2());
  35. $cb = $ca->bindTo($b, NULL);
  36. var_dump($cb());
  37. ?>
  38. --EXPECTF--
  39. int(1)
  40. Testing with scope given as object
  41. int(21)
  42. int(22)
  43. Testing with scope as string
  44. int(23)
  45. int(24)
  46. Fatal error: Uncaught Error: Cannot access private property B::$x in %s:%d
  47. Stack trace:
  48. #0 %s(%d): Closure->{closure}()
  49. #1 {main}
  50. thrown in %s on line %d