1.2.645. Lost References

Either avoid references, or propagate them correctly.

When assigning a referenced variable with another reference, the initial reference is lost, while the intend was to transfer the content. Do not reassign a reference with another reference. Assign new content to the reference to change its value.

<?php

function foo(&$lostReference, &$keptReference)
{
    $c = 'c';

    // $lostReference was a reference to $bar, but now, it is a reference to $c
    $lostReference =& $c;
    // $keptReference was a reference to $bar : it is still now, though it contains the actual value of $c now
    $keptReference = $c;
}

$bar = 'bar';
$bar2 = 'bar';
foo($bar, $bar2);

//displays bar c, instead of bar bar
print $bar. ' '.$bar2;

?>

1.2.645.1. Suggestions

  • Always assign new value to an referenced argument, and don’t reassign a new reference

1.2.645.2. Specs

Short name

Variables/LostReferences

Rulesets

All, Analyze, Changed Behavior

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Features

reference

Examples

WordPress

Available in

Entreprise Edition, Exakat Cloud