1.2.834. No Reference For Ternary¶
The ternary operator and the null coalescing operator are both expressions that only return values, and not a reference.
This means that any provided reference will be turned into its value. While this is usually invisible, it will raise a warning when a reference is expected. This is the case with methods returning a reference.
A PHP notice is generated when using a ternary operator or the null coalesce operator : Only variable references should be returned by reference
. The notice is also emitted when returning objects.
This applies to methods, functions and closures.
<?php
// This works
function &foo($a, $b) {
if ($a === 1) {
return $b;
} else {
return $a;
}
}
// This raises a warning, as the operator returns a value
function &foo($a, $b) { return $a === 1 ? $b : $a; }
?>
See also Null Coalescing Operator and Ternary Operator.
1.2.834.2. Connex PHP features¶
1.2.834.2.1. Suggestions¶
Drop the reference at assignation time
Drop the reference in the argument definition
Drop the reference in the function return definition
1.2.834.2.2. Specs¶
Short name |
Php/NoReferenceForTernary |
Rulesets |
|
Exakat since |
1.0.8 |
PHP Version |
All |
Severity |
Major |
Time To Fix |
Slow (1 hour) |
Precision |
Very high |
Examples |
|
Available in |