[php-src] Issue #7943: Nested Attributes values are not static through engine life
| From: | aurimasniekis | Date: | Fri, 14 Jan 2022 18:20:18 +0000 |
| Subject: | [php-src] Issue #7943: Nested Attributes values are not static through engine life | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-239013@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/7943
Author: aurimasniekis
### 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
<?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"
}
}
```
[3v4l.org](https://3v4l.org/Hv5N4#v8.1.1)
### PHP Version
8.1.1
### Operating System
_No response_