3v4l.org

run code in 500+ PHP versions simultaneously
<?php class RandomValue { public string $value; public function __construct(string $prefix) { $this->value = $prefix . random_int(1,100); } } #[Attribute] class Foo { public function __construct(public readonly RandomValue $bar) {} } #[Foo(new RandomValue('FooBar: '))] class Bar { } $ref = new ReflectionClass(Bar::class); $attribute = $ref->getAttributes()[0]; var_dump($attribute->getArguments()); var_dump($attribute->getArguments()); var_dump($attribute->getArguments());
Output for 8.1.1
array(1) { [0]=> object(RandomValue)#3 (1) { ["value"]=> string(10) "FooBar: 33" } } array(1) { [0]=> object(RandomValue)#3 (1) { ["value"]=> string(10) "FooBar: 35" } } array(1) { [0]=> object(RandomValue)#3 (1) { ["value"]=> string(10) "FooBar: 45" } }

preferences:
16.04 ms | 1164 KiB | 6 Q