3v4l.org

run code in 300+ PHP versions simultaneously
<?php #[Attribute(Attribute::TARGET_PROPERTY)] class SomeAttribute { } class Foo { public function __construct( #[SomeAttribute] public readonly string $someProperty, ) {} } $foo = new Foo('foobar'); $reflectedProperty = new ReflectionProperty($foo, 'someProperty'); var_dump($reflectedProperty->getAttributes()[0]->getName()); $reflectedMethod = new ReflectionMethod($foo, '__construct'); var_dump($reflectedMethod->getParameters()[0]->getAttributes()[0]->getName());
Output for 8.1.0 - 8.1.11
string(13) "SomeAttribute" string(13) "SomeAttribute"
Output for 8.0.1 - 8.0.24
Parse error: syntax error, unexpected identifier "string", expecting variable in /in/SlhAP on line 12
Process exited with code 255.
Output for 7.4.0 - 7.4.32
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting variable (T_VARIABLE) in /in/SlhAP on line 12
Process exited with code 255.

preferences:
195.3 ms | 1395 KiB | 73 Q