001_placement.phpt 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. --TEST--
  2. Attributes can be placed on all supported elements.
  3. --FILE--
  4. <?php
  5. #[A1(1)]
  6. class Foo
  7. {
  8. #[A1(2)]
  9. public const FOO = 'foo';
  10. #[A1(3)]
  11. public $x;
  12. #[A1(4)]
  13. public function foo(#[A1(5)] $a, #[A1(6)] $b) { }
  14. }
  15. $object = new #[A1(7)] class () { };
  16. #[A1(8)]
  17. function f1() { }
  18. $f2 = #[A1(9)] function () { };
  19. $f3 = #[A1(10)] fn () => 1;
  20. $ref = new \ReflectionClass(Foo::class);
  21. $sources = [
  22. $ref,
  23. $ref->getReflectionConstant('FOO'),
  24. $ref->getProperty('x'),
  25. $ref->getMethod('foo'),
  26. $ref->getMethod('foo')->getParameters()[0],
  27. $ref->getMethod('foo')->getParameters()[1],
  28. new \ReflectionObject($object),
  29. new \ReflectionFunction('f1'),
  30. new \ReflectionFunction($f2),
  31. new \ReflectionFunction($f3)
  32. ];
  33. foreach ($sources as $r) {
  34. $attr = $r->getAttributes();
  35. var_dump(get_class($r), count($attr));
  36. foreach ($attr as $a) {
  37. var_dump($a->getName(), $a->getArguments());
  38. }
  39. echo "\n";
  40. }
  41. ?>
  42. --EXPECT--
  43. string(15) "ReflectionClass"
  44. int(1)
  45. string(2) "A1"
  46. array(1) {
  47. [0]=>
  48. int(1)
  49. }
  50. string(23) "ReflectionClassConstant"
  51. int(1)
  52. string(2) "A1"
  53. array(1) {
  54. [0]=>
  55. int(2)
  56. }
  57. string(18) "ReflectionProperty"
  58. int(1)
  59. string(2) "A1"
  60. array(1) {
  61. [0]=>
  62. int(3)
  63. }
  64. string(16) "ReflectionMethod"
  65. int(1)
  66. string(2) "A1"
  67. array(1) {
  68. [0]=>
  69. int(4)
  70. }
  71. string(19) "ReflectionParameter"
  72. int(1)
  73. string(2) "A1"
  74. array(1) {
  75. [0]=>
  76. int(5)
  77. }
  78. string(19) "ReflectionParameter"
  79. int(1)
  80. string(2) "A1"
  81. array(1) {
  82. [0]=>
  83. int(6)
  84. }
  85. string(16) "ReflectionObject"
  86. int(1)
  87. string(2) "A1"
  88. array(1) {
  89. [0]=>
  90. int(7)
  91. }
  92. string(18) "ReflectionFunction"
  93. int(1)
  94. string(2) "A1"
  95. array(1) {
  96. [0]=>
  97. int(8)
  98. }
  99. string(18) "ReflectionFunction"
  100. int(1)
  101. string(2) "A1"
  102. array(1) {
  103. [0]=>
  104. int(9)
  105. }
  106. string(18) "ReflectionFunction"
  107. int(1)
  108. string(2) "A1"
  109. array(1) {
  110. [0]=>
  111. int(10)
  112. }