Only variables should be passed by reference¶
Description¶
Methods arguments may be passed by value or by reference. The first one is the default behavior, and the second one is optional: it is identified with the &
, before the name of the argument.
When an argument is passed by reference, the calling context passes a variable, and conserves a handle to the modified variable after the execution of the method. The handle points to a data container, such as a variable, a property, a static property or a array item.
When passing literal values or constants, global or class, a distinct error is emitted, as there cannot be a reference to them. They can only be passed by value.
This error message is emitted when a value is returned, and feed into a reference argument.
Example¶
<?php
function foo(int &$int) {
// doSomething
}
// the result of a function is still a literal value
foo(abs(-3));
// a direct literal value generates that error
foo(3);
?>
Solutions¶
Store the value in a variable, and pass the variable to the method.
Remove the reference from the called method signature.
In previous PHP versions, this error message used to be Only variables can be passed by reference.