1.2.867. Objects Don’t Need References

There is no need to add references to parameters for objects, as those are always passed by reference when used as arguments.

Reference operator is needed when the object are replaced inside the method with a new value (or a clone), as whole. Calls to methods or property modifications do not require extra reference.

Reference operator is also needed when one of the types is scalar : this include null, and the hidden null type : that is when the default value is null.

This rule applies to arguments in methods, and to foreach() blind variables.

<?php
    $object = new stdClass();
    $object->name = 'a';

    foo($object);
    print $object->name; // Name is 'b'

    // No need to make $o a reference
    function foo(&$o) {
        $o->name = 'b';
    }

    // $o is assigned inside the function : the parameter must have a &, or the new object won't make it out of the foo3 scope
    function foo3(&$o) {
        $o = new stdClass;
    }

    $array = array($object);
    foreach($array as &$o) { // No need to make this a reference
        $o->name = 'c';
    }

?>

See also Passing by reference.

1.2.867.1. Suggestions

  • Remove the reference

  • Assign the argument with a new value

1.2.867.2. Specs

Short name

Structures/ObjectReferences

Rulesets

All, Analyze, CE, CI-checks, Top10

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

High

Features

reference

ClearPHP

no-references-on-objects

Examples

Zencart, XOOPS

Available in

Entreprise Edition, Community Edition, Exakat Cloud