Closed as not planned
Closed as not planned
Description
Description
If an Attribute that only targets Parameters is applied to a Promoted Property in a constructor, reflection explodes throws an exception on reflecting property attributes.
I am opening this ticket b/c Gina Peter Banyard said I should - https://phpc.social/@Girgias/114423870731867147
The following code:
<?php
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class Inject {
public function __construct( public readonly string $id ) { }
}
class MyFoo {
public function __construct(
#[Inject('baz-value')]
public readonly int $bar,
){}
}
$ref = new ReflectionClass(MyFoo::class);
echo "Checking Parameters : ";
$parameters = $ref->getConstructor()->getParameters();
foreach ($parameters as $parameter) {
foreach ($parameter->getAttributes() as $attribute) {
$attribute->newInstance();
}
}
echo "✅\n\n";
echo "Checking Properties : ";
$properties = $ref->getProperties();
foreach ($properties as $property) {
foreach ($property->getAttributes() as $attribute) {
$attribute->newInstance();
}
}
echo "✅\n\n";
Resulted in this output:
Checking Parameters : ✅
Checking Properties :
Fatal error: Uncaught Error: Attribute "Inject" cannot target property (allowed targets: parameter) in /in/rMqqA:30
Stack trace:
#0 /in/rMqqA(30): ReflectionAttribute->newInstance()
#1 {main}
thrown in /in/rMqqA on line 30
Process exited with code 255.
But I expected this output instead:
Checking Parameters : ✅
Checking Properties : ✅
PHP Version
PHP 8.4.6 (cli) (built: Apr 8 2025 19:55:31) (NTS)
Copyright (c) The PHP Group
Built by Shivam Mathur
Zend Engine v4.4.6, Copyright (c) Zend Technologies
with Zend OPcache v8.4.6, Copyright (c), by Zend Technologies
It seems to be an issue on 8.1+ - https://3v4l.org/rMqqA
Operating System
No response