3v4l.org

run code in 300+ PHP versions simultaneously
<?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";
Output for 8.2.0 - 8.2.28, 8.3.0 - 8.3.21, 8.4.1 - 8.4.7
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.

preferences:
41.31 ms | 406 KiB | 5 Q