错误信息“严格标准:只应通过引用传递变量" [英] Error message "Strict standards: Only variables should be passed by reference"

查看:29
本文介绍了错误信息“严格标准:只应通过引用传递变量"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

上面的代码以某种方式报告了严格的标准警告,但这不会:

The above code somehow reports the strict standards 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?

推荐答案

考虑以下代码:

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());

这将生成以下输出:

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

原因?test::get_arr() 方法不是变量,在严格模式下会产生警告.这种行为非常不直观,因为 get_arr() 方法返回一个数组值.

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);
}

既然你不能改变array_shift的签名,你也可以使用一个中间变量:

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

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

这篇关于错误信息“严格标准:只应通过引用传递变量"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆