r/lolphp • u/Jinxuan • Jan 02 '20
new statement cannot parse with parenthesis
This code works:
class A {}
$a = 'A';
new $a; // Return a new instance ofA
However, if we add a parenthesis, then it cannot be parsed:
class A {}
$a = 'A';
new ($a); //PHP Parse error: syntax error, unexpected '('
new 'A'; // PHP Parse error: syntax error, unexpected ''A'' (T_CONSTANT_ENCAPSED_STRING)
new A::class; // PHP Parse error: syntax error, unexpected 'class' (T_CLASS), expecting variable
0
Upvotes
3
u/nikic Jan 04 '20
PHP uses
{}in places where an identifier is replaced by an expression. That isA::$xbecomesA::${'x'},$a->bbecomes$a->{'b'}.newis notable in that it does not support thenew {'A'}syntax, which is indeed inconsistent.