find(..)) 上面的代码以某种方式报告了严格标准的警告,但这不会: function get_arr(){ return arr-6ren">
gpt4 book ai didi

php - 错误消息 "Strict standards: Only variables should be passed by reference"

转载 作者:IT老高 更新时间:2023-10-28 11:56:43 27 4
gpt4 key购买 nike

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

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

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

那么它什么时候会报告警告呢?

最佳答案

考虑以下代码:

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() 方法返回 一个数组值。

要在严格模式下解决此错误,请更改方法的签名以使其不使用引用:

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

由于您无法更改 array_shift 的签名,因此您也可以使用中间变量:

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

关于php - 错误消息 "Strict standards: Only variables should be passed by reference",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2354609/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com