<?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
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.