错误信息“Strict Standards: Only Variables Should Be Passed by Reference”
使用 array_shift() 时,可能会报告严格标准如果传递的参数是函数调用的结果,则会发出警告。这种行为看起来不一致,因为它并不总是触发警告。
考虑以下代码:
$el = array_shift($instance->find(..))
在此示例中,引发警告是因为 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());
此代码生成严格标准警告,因为 $t->get_arr() 方法不是变量并且是通过引用传递的。此行为是违反直觉的,因为该方法返回一个数组值。
要在严格模式下避免此错误,请更改方法签名以避免使用引用或使用中间变量:
function test_arr($a) { var_dump($a); } $inter = get_arr(); $el = array_shift($inter);
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3