3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { #[MyAttr(msg: __PROPERTY__)] public string $i = __PROPERTY__ { #[MyAttr(msg: __PROPERTY__)] get { var_dump(__PROPERTY__); // dumps "i" return $this->i; } set ( #[MyAttr(msg: __PROPERTY__)] string $v ) { $this->i = $v; } } } $f = new Foo(); var_dump($f->i); // dumps "" because it does not work in default value $r = new ReflectionClass(Foo::class); $p = $r->getProperty('i'); var_dump($p->getAttributes()[0]->getArguments()); // dumps "" because it does not work in property attributes $get = $p->getHook(PropertyHookType::Get); var_dump($get->getAttributes()[0]->getArguments()); // dumps "" because it does not work in hook attributes $set = $p->getHook(PropertyHookType::Set); var_dump($set->getParameters()[0]->getAttributes()[0]->getArguments()); // dumps "i" because it only works in parameter attributes
Output for 8.4.4
string(1) "i" string(1) "i" array(1) { ["msg"]=> string(1) "i" } array(1) { ["msg"]=> string(1) "i" } array(1) { ["msg"]=> string(1) "i" }
Output for 8.4.2
string(1) "i" string(0) "" array(1) { ["msg"]=> string(0) "" } array(1) { ["msg"]=> string(0) "" } array(1) { ["msg"]=> string(1) "i" }
Output for 8.0.30
Parse error: syntax error, unexpected identifier "get", expecting "function" or "fn" or "static" or "#[" in /in/4BMJt on line 9
Process exited with code 255.
Output for 7.2.34
Parse error: syntax error, unexpected 'string' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/4BMJt on line 7
Process exited with code 255.
Output for 7.0.0
Parse error: syntax error, unexpected 'string' (T_STRING), expecting variable (T_VARIABLE) in /in/4BMJt on line 7
Process exited with code 255.
Output for 5.2.17
Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in /in/4BMJt on line 7
Process exited with code 255.
Output for 4.3.0
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/4BMJt on line 7
Process exited with code 255.

preferences:
163.83 ms | 1007 KiB | 7 Q