syntax error, unexpected token “;”, expecting “->” or “?->” or “[”¶
Description¶
The &
reference operator turns a value into a reference. It works with data containers, such as variables, properties or array elements. It doesn’t work on literal constants.
The error message suggestion to use extra operators such as [
or ->
leads to a distinct error: Cannot use temporary expression in write context
.
This error is also raised with a reference on a new
operator. The problem is on the reference operator, but the parser tries to understand more details later in the code before coming back to it.
Example¶
<?php
$a = &E_ALL;
const B = [1,2,3];
//Cannot use temporary expression in write context
$c = &B[2];
// The error is actually on the &
$x = & new X();
?>
Solutions¶
Store the constant in a variable, and make the reference on the variable.
Use constants by value, not by reference.
Remove the reference operator
&
before the new call.