1.2.807. No Literal For Reference

Method arguments and return values may be by reference. Then, they need to be a valid variable.

Objects are always passed by reference, so there is no need to explicitly declare it.

Expressions, including ternary operator, produce value, and can’t be used by reference directly. This is also the case for expression that include one or more reference.

Wrongly passing a value as a reference leads to a PHP Notice.

<?php

// variables, properties, static properties, array items are all possible
$a = 1;
foo($a);

//This is not possible, as a literal can't be a reference
foo(1);

function foo(&$int) { return $int; }


// This is not a valid reference
function &bar() { return 2; }
function &bar2() { return 2 + $r; }

?>

See also References.

1.2.807.1. Suggestions

  • Remove the reference in the method signature (argument or return value)

  • Make the argument an object, by using a typehint (non-scalar)

  • Put the value into a variable prior to call (or return) the method

1.2.807.2. Specs

Short name

Functions/NoLiteralForReference

Rulesets

All, Analyze, CE, CI-checks, Changed Behavior

Exakat since

1.9.5

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

reference, literal

Available in

Entreprise Edition, Community Edition, Exakat Cloud