011_inheritance.phpt 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. --TEST--
  2. Attributes comply with inheritance rules.
  3. --FILE--
  4. <?php
  5. #[A2]
  6. class C1
  7. {
  8. #[A1]
  9. public function foo() { }
  10. }
  11. class C2 extends C1
  12. {
  13. public function foo() { }
  14. }
  15. class C3 extends C1
  16. {
  17. #[A1]
  18. public function bar() { }
  19. }
  20. $ref = new \ReflectionClass(C1::class);
  21. print_r(array_map(fn ($a) => $a->getName(), $ref->getAttributes()));
  22. print_r(array_map(fn ($a) => $a->getName(), $ref->getMethod('foo')->getAttributes()));
  23. $ref = new \ReflectionClass(C2::class);
  24. print_r(array_map(fn ($a) => $a->getName(), $ref->getAttributes()));
  25. print_r(array_map(fn ($a) => $a->getName(), $ref->getMethod('foo')->getAttributes()));
  26. $ref = new \ReflectionClass(C3::class);
  27. print_r(array_map(fn ($a) => $a->getName(), $ref->getAttributes()));
  28. print_r(array_map(fn ($a) => $a->getName(), $ref->getMethod('foo')->getAttributes()));
  29. echo "\n";
  30. trait T1
  31. {
  32. #[A2]
  33. public $a;
  34. }
  35. class C4
  36. {
  37. use T1;
  38. }
  39. class C5
  40. {
  41. use T1;
  42. public $a;
  43. }
  44. $ref = new \ReflectionClass(T1::class);
  45. print_r(array_map(fn ($a) => $a->getName(), $ref->getProperty('a')->getAttributes()));
  46. $ref = new \ReflectionClass(C4::class);
  47. print_r(array_map(fn ($a) => $a->getName(), $ref->getProperty('a')->getAttributes()));
  48. $ref = new \ReflectionClass(C5::class);
  49. print_r(array_map(fn ($a) => $a->getName(), $ref->getProperty('a')->getAttributes()));
  50. ?>
  51. --EXPECT--
  52. Array
  53. (
  54. [0] => A2
  55. )
  56. Array
  57. (
  58. [0] => A1
  59. )
  60. Array
  61. (
  62. )
  63. Array
  64. (
  65. )
  66. Array
  67. (
  68. )
  69. Array
  70. (
  71. [0] => A1
  72. )
  73. Array
  74. (
  75. [0] => A2
  76. )
  77. Array
  78. (
  79. [0] => A2
  80. )
  81. Array
  82. (
  83. )