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

标签 php reference strict

$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/40290404/

相关文章:

MySQL 在 where 子句中使用带有子索引的严格模式

JavaScript 'strict mode' 没有按预期工作?

PHP session 不存储值

PHP 使用相同的 $result 资源进行多种排序

javascript - 解决可能的严格违规(并帮助 bat 侠拯救哥谭)

Java实例变量的内存分配

c++ - 通过引用传递 vector 并在基于范围的 for 循环中更改其值?

php - AWS Elastic Beanstalk - 如何在同一个 ec2 实例中运行 php 和 nodejs

php - 如何用C++开发PHP的pear

c++ - 引用类型转换运算符 : asking for trouble?