new_in_attributes.phpt 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. --TEST--
  2. new in attribute arguments
  3. --FILE--
  4. <?php
  5. #[Attribute]
  6. class MyAttribute {
  7. public function __construct(public $x, public $y) {}
  8. }
  9. #[MyAttribute(null, new stdClass)]
  10. class Test1 {
  11. }
  12. $rc = new ReflectionClass(Test1::class);
  13. $ra = $rc->getAttributes()[0];
  14. $args1 = $ra->getArguments();
  15. $obj1 = $ra->newInstance();
  16. var_dump($args1, $obj1);
  17. // Check that we get fresh instances each time:
  18. $args2 = $ra->getArguments();
  19. $obj2 = $ra->newInstance();
  20. var_dump($args1[1] !== $args2[1]);
  21. var_dump($obj1->y !== $obj2->y);
  22. // Check that named args work:
  23. #[MyAttribute(y: new stdClass, x: null)]
  24. class Test2 {
  25. }
  26. $rc = new ReflectionClass(Test2::class);
  27. $ra = $rc->getAttributes()[0];
  28. $args = $ra->getArguments();
  29. $obj = $ra->newInstance();
  30. var_dump($args, $obj);
  31. ?>
  32. --EXPECT--
  33. array(2) {
  34. [0]=>
  35. NULL
  36. [1]=>
  37. object(stdClass)#3 (0) {
  38. }
  39. }
  40. object(MyAttribute)#4 (2) {
  41. ["x"]=>
  42. NULL
  43. ["y"]=>
  44. object(stdClass)#5 (0) {
  45. }
  46. }
  47. bool(true)
  48. bool(true)
  49. array(2) {
  50. ["y"]=>
  51. object(stdClass)#2 (0) {
  52. }
  53. ["x"]=>
  54. NULL
  55. }
  56. object(MyAttribute)#10 (2) {
  57. ["x"]=>
  58. NULL
  59. ["y"]=>
  60. object(stdClass)#11 (0) {
  61. }
  62. }