Strict Standards: Only variables should be passed by reference

本小妞迷上赌 提交于 2019-11-25 21:45:11

问题


$el = array_shift($instance->find(..))

The above code somehow reports the strict standars warning,but this will not:

function get_arr(){
    return array(1,2);
}
$el = array_shift(get_arr());

So when will it report the warning anyway?


回答1:


Consider the following code:

error_reporting(E_STRICT);
class test {
    function test_arr(&$a) {
        var_dump($a);   
    }
    function get_arr() {
        return array(1,2);  
    }
}

$t= new test;
$t->test_arr($t->get_arr());

This will generate the following output:

Strict Standards: Only variables should be passed by reference in `test.php` on line 14
array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}

The reason? The test::get_arr() method is not a variable and under strict mode this will generate a warning. This behavior is extremely non-intuitive as the get_arr() method returns an array value.

To get around this error in strict mode either change the signature of the method so it doesn't use a reference:

function test_arr($a) {
    var_dump($a);  
}

Since you can't change the signature of array_shift you can also use an intermediate variable:

$inter= get_arr();
$el= array_shift($inter);



回答2:


$instance->find() returns reference to variable.

You get the report when you are trying to use this reference as an argument to function,without storing it at variable first.

This helps preventing memory leaks, and will probably become error in next PHP versions.

Your 2nd code would throw error if it wrote like (note the & in function signature):

function &get_arr(){
    return array(1,2);
}
$el = array_shift(get_arr());

So a quick (and not so nice) fix would be:

$el = array_shift($tmp = $instance->find(..));

Basically you do an assignment to temporary variable first, and send the variable as an argument.




回答3:


The cause of the error is the use of the internal PHP Programming Data Structures function, array_shift() [ php.net/end ].

The function takes an array as a parameter. Although an ampersand is indicated in the prototype of array_shift() in The Manual", there is no cautionary documentation following in the extended definition of that function, nor is there any apparent explanation that the parameter is in fact passed by reference.

Perhaps this is /understood/. I did not understand, however, so it was difficult for me to detect the cause of the error.

Reproduce code:

function get_arr()
{
 return array(1,2);
}
$array = get_arr();
$el = array_shift($array);



回答4:


The second snippet doesn't work either and that's why. array_shift is a modifier function, that changes its argument, therefore it expects its parameter to be a reference, and you cannot reference something that is not a variable. See Rasmus' explanations here: Strict standards: Only variables should be passed by reference




回答5:


This code:

$monthly_index = array_shift(unpack('H*', date('m/Y')));

Need to be changed into:

$date_time = date('m/Y');
$unpack = unpack('H*', $date_time);
array_shift($unpack);



回答6:


Well, in obvious cases like that, you can always tell PHP to suppress messages by using "@" in front of the function.

$monthly_index = @array_shift(unpack('H*', date('m/Y')));

It may not be one of the best programming practices to suppress all errors this way, but in certain cases (like this one) it comes handy and is acceptable.

As result, I am sure your friend SysAdmin will be pleased with a less polluted error.log



来源:https://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!