closure_call.phpt 948 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. --TEST--
  2. Closure::call
  3. --FILE--
  4. <?php
  5. class Foo {
  6. public $x = 0;
  7. function bar() {
  8. return function () {
  9. return $this->x;
  10. };
  11. }
  12. }
  13. $foo = new Foo;
  14. $qux = $foo->bar();
  15. $foobar = new Foo;
  16. $foobar->x = 3;
  17. var_dump($qux());
  18. var_dump($qux->call($foo));
  19. // Try on an object other than the one already bound
  20. var_dump($qux->call($foobar));
  21. $bar = function () {
  22. return $this->x;
  23. };
  24. $elePHPant = new StdClass;
  25. $elePHPant->x = 7;
  26. // Try on a StdClass
  27. var_dump($bar->call($elePHPant));
  28. $beta = function ($z) {
  29. return $this->x * $z;
  30. };
  31. // Ensure argument passing works
  32. var_dump($beta->call($foobar, 7));
  33. // Ensure ->call calls with scope of passed object
  34. class FooBar {
  35. private $x = 3;
  36. }
  37. $foo = function () {
  38. var_dump($this->x);
  39. };
  40. $foo->call(new FooBar);
  41. ?>
  42. --EXPECTF--
  43. int(0)
  44. int(0)
  45. int(3)
  46. Warning: Cannot bind closure to scope of internal class stdClass in %s line %d
  47. NULL
  48. int(21)
  49. int(3)