%s(): Argument #%d%s%s%s could not 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, this 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
}
foo(-3);
?>
Literal Examples¶
foo(): Argument #3 ($int) could not be passed by reference
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 %s(): Argument #%d%s%s%s cannot not be passed by reference.