Only variables should be passed by reference

Events happening in the community are now at Drupal community events on www.drupal.org.
mfb's picture

As you know if you've fiddled with PHP's error_reporting setting, Drupal 7 is not yet E_STRICT compliant. E_STRICT compliance is a good idea for forward compatibility (PHP 5.3 and 6.0) and also helpful for servers that already log E_STRICT errors.

Many E_STRICT errors are evaluated at compile time, so E_STRICT error reporting should be enabled not in PHP code, but rather in the PHP configuration (e.g. php.ini file): error_reporting = E_ALL | E_STRICT

The most prevalent E_STRICT issue in core is numerous attempts to pass an expression (such as a function call), rather than a variable, by reference. This can be seen for example in the drupal_render($function()) pattern, e.g. drupal_render(drupal_get_form('some_form')). Running this code with E_STRICT error reporting results in: "Strict standards: Only variables should be passed by reference..."

The issue is that for functions like reset($array) or drupal_render($array), the array parameter is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function (or expression) returning an array, because only actual variables can be passed by reference.

We can easily remedy this code snippet by creating an intermediate variable for the array:

<?php
$elements
= drupal_get_form('some_form');
drupal_render($elements);
?>

A patch fixing these and other E_STRICT errors is being maintained at http://drupal.org/node/348448

Comments

THANK YOU

deryck.henson's picture

This is two years old. Long Hair.

2nd most useful thing I've found with this search box.

Coding standards

Group organizers

Group categories

Status

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week