Nested Attributes values are not static through engine life
Description
I am not really sure if this is a bug of Nested Attributes, but if I understood correctly attributes must be constant expressions (aka static values) but there is the possibility that some calculations would happen inside nested attribute constructor which would lead to some not really static value. This would be fine if that value would remain the same throughout the whole engine life. But by Attributes RFC definition the reflection methods getArguments and newInstance initializes every time from scratch which makes this really unpredictable. Because each call to them produces a different result.
This makes reflection/static analysis of attribute values inconsistent.
The following code:
<?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());
Resulted in this output:
array(1) {
[0]=>
object(RandomValue)#3 (1) {
["value"]=>
string(10) "FooBar: 44"
}
}
array(1) {
[0]=>
object(RandomValue)#3 (1) {
["value"]=>
string(10) "FooBar: 21"
}
}
array(1) {
[0]=>
object(RandomValue)#3 (1) {
["value"]=>
string(10) "FooBar: 62"
}
}
But I expected this output instead:
array(1) {
[0]=>
object(RandomValue)#3 (1) {
["value"]=>
string(10) "FooBar: 44"
}
}
array(1) {
[0]=>
object(RandomValue)#3 (1) {
["value"]=>
string(10) "FooBar: 44"
}
}
array(1) {
[0]=>
object(RandomValue)#3 (1) {
["value"]=>
string(10) "FooBar: 44"
}
}
PHP Version
8.1.1
Operating System
No response
This is working as intended. Yes, you can do stupid things if you like. You don't need nested attributes for this either, you can call random_int() in a normal attribute constructor if you like.
Yes, I understand you can do stupid things, but my point was more about getArguments which returns values for that attribute, and the constructor of that attribute doesn't really matter what it does.
My main point is that if you pass something as an argument to an attribute constructor, in this case, some random object (the random_int was just to prove that object changes), the reflection of that attribute when using getArguments should return the same result at least for the same ReflectionAttribute class instance, or best for that engine lifetime.
Because if you try to interact with that object instance, and then call somewhere getArguments again it will be replaced as it's not the same instance.
I have checked ReflectionParameter and it behaves exactly the same, and I kinda understand the point of it.
The outer problem of this was that with getArguments now returns object instance of that new Object() value as an argument, which makes trying to figure out how that new Object(.....) was constructed impossible, because if that Object has some kind of logic inside constructor you would not be able to recreate the same state of the instance.
Even if by collecting properties and repopulating them back would not mean it would be correct because if the constructor has some kind of external logic for e.g. if today is Sunday value is such otherwise different.