ReflectionFunction_getClosureCalledClass.phpt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. --TEST--
  2. GH-8932 (Provide a way to get the called-scope of closures)
  3. --FILE--
  4. <?php
  5. class A {
  6. public static function __callStatic($name, $args) {
  7. echo static::class.'::'.$name, "\n";
  8. }
  9. public function __call($name, $args) {
  10. echo static::class.'->'.$name, "\n";
  11. }
  12. public static function b() {
  13. echo static::class.'::b', "\n";
  14. }
  15. public function c() {
  16. echo static::class.'->c', "\n";
  17. }
  18. public function makeClosure() {
  19. return function () {
  20. echo static::class.'::{closure}'."\n";
  21. };
  22. }
  23. }
  24. class B extends A {}
  25. $c = ['B', 'b'];
  26. $d = \Closure::fromCallable($c);
  27. $r = new \ReflectionFunction($d);
  28. var_dump($r->getClosureCalledClass());
  29. $d();
  30. $c = [new B(), 'c'];
  31. $d = \Closure::fromCallable($c);
  32. $r = new \ReflectionFunction($d);
  33. var_dump($r->getClosureCalledClass());
  34. $d();
  35. $c = ['B', 'd'];
  36. $d = \Closure::fromCallable($c);
  37. $r = new \ReflectionFunction($d);
  38. var_dump($r->getClosureCalledClass());
  39. $d();
  40. $c = [new B(), 'e'];
  41. $d = \Closure::fromCallable($c);
  42. $r = new \ReflectionFunction($d);
  43. var_dump($r->getClosureCalledClass());
  44. $d();
  45. $c = ['A', 'b'];
  46. $d = \Closure::fromCallable($c);
  47. $r = new \ReflectionFunction($d);
  48. var_dump($r->getClosureCalledClass());
  49. $d();
  50. $b = new B();
  51. $d = $b->makeClosure();
  52. $r = new \ReflectionFunction($d);
  53. var_dump($r->getClosureCalledClass());
  54. $d();
  55. $d = function () {
  56. echo "{closure}\n";
  57. };
  58. $r = new \ReflectionFunction($d);
  59. var_dump($r->getClosureCalledClass());
  60. $d();
  61. ?>
  62. --EXPECTF--
  63. object(ReflectionClass)#%d (1) {
  64. ["name"]=>
  65. string(1) "B"
  66. }
  67. B::b
  68. object(ReflectionClass)#%d (1) {
  69. ["name"]=>
  70. string(1) "B"
  71. }
  72. B->c
  73. object(ReflectionClass)#%d (1) {
  74. ["name"]=>
  75. string(1) "B"
  76. }
  77. B::d
  78. object(ReflectionClass)#%d (1) {
  79. ["name"]=>
  80. string(1) "B"
  81. }
  82. B->e
  83. object(ReflectionClass)#%d (1) {
  84. ["name"]=>
  85. string(1) "A"
  86. }
  87. A::b
  88. object(ReflectionClass)#%d (1) {
  89. ["name"]=>
  90. string(1) "B"
  91. }
  92. B::{closure}
  93. NULL
  94. {closure}