Only variables can be passed by reference

2019-01-18 19:38发布

问题:

I had the bright idea of using a custom error handler which led me down a rabbit hole.

Following code gives (with and without custom error handler): Fatal error: Only variables can be passed by reference

function foo(){
    $b=array_pop(array("a","b","c"));
    return $b;
}
print_r(foo());

Following code gives (only with a custom error handler): (2048) Only variables should be passed by reference

function foo(){
    $a=explode( '/' , 'a/b/c');
    $c=array_pop(array_slice($a,-2,1));
    return $c;
}
print_r(foo());

The second one worries me since I have a lot of 'compact' code. So, I either ditch the bright idea of using a custom error handler (to improve my logging module) or expand all my code.

Anyone with better ideas? Also, WTF?

UPDATE:

Thanks to the answers I've learnt something about how php does error handling. The confusion of E_ALL not including E_STRICT (php 5) is not cool.

On top of all this, creating your own custom error handler enables E_STRICT by default and thats where problems start.

The moral of the story is to use your own error handler to catch them ALL and use the error constants (E_STRICT, E_USER_WARNING, E_USER_ERROR, etc.) to do your filtering.

As for the 'memory corruption issue' with variable references and certain functions, what can I say? Doubly uncool. I'll (which doesn't mean you should) ignore E_STRICT in my error handler and life goes on.

回答1:

array_pop() tries to change that value which is passed as parameter. Now in your second example this is the return value from array_slice(). In engine terms this is a "temporary value" and such a value can't be passed by references. what you need is a temporary variable:

function foo(){
    $a=explode( '/' , 'a/b/c');
    $b=array_slice($a,-2,1);
    $c=array_pop($b);
    return $c;
}
print_r(foo());

Then a reference to $b can be passed to array_pop(). See http://php.net/references for more details on references.



回答2:

Here is what I get when trying your second php code snippet in php-cli after setting error_reporting to E_ALL | E_STRICT

    gparis@techosaure:~/workspace/universcine.com$ php -a
Interactive shell

php > function foo(){
php {     $a=explode( '/' , 'a/b/c');
php {     $c=array_pop(array_slice($a,-2,1));
php {     return $c;
php { }
php > print_r(foo());
PHP Strict standards:  Only variables should be passed by reference in php shell code on line 3
PHP Stack trace:
PHP   1. {main}() php shell code:0
PHP   2. foo() php shell code:1

As you can see, it's only strict standards here. And you can easily let your custom error handler ignore them (based on the value you get : 2048 for instance, here).

As of php 5.3, E_ALL does not include E_STRICT, look at this :

php > foreach(array("E_ALL", "E_DEPRECATED", "E_STRICT", "E_NOTICE", "E_PARSE", "E_WARNING") as $const) echo $const . "  :\t" . constant($const) ."\t". decbin(constant($const)). "\n";
E_ALL  :        30719   111011111111111
E_DEPRECATED  : 8192     10000000000000
E_STRICT  :     2048       100000000000
E_NOTICE  :     8                  1000
E_PARSE  :      4                   100
E_WARNING  :    2                    10

As of php 5.4, E_ALL does include E_STRICT :

E_ALL  :            32767   111111111111111
E_DEPRECATED  :     8192     10000000000000
E_STRICT  :         2048       100000000000
E_NOTICE  :         8                  1000
E_PARSE  :          4                   100
E_WARNING  :        2                    10


回答3:

It's a memory corruption issue (according to PHP dev team). Just throw in an assignment:

function foo(){
    $b = array_pop($arr = array("a","b","c"));
    return $b;
}
print_r(foo());

:

function foo(){
    $a = explode( '/' , 'a/b/c');
    $c = array_pop($arr = array_slice($a,-2,1));
    return $c;
}
print_r(foo());

The second produces an E_STRICT. You can handle that differently in your error handler if you wish (if you don't want to change those functions).



回答4:

array_pop() changes that value passed to it which is where the error is coming from. A function cannot be changed. In other words, you need to assign the array to a variable first (ref: manual), and then run array_pop().

The code you need is this:

function foo(){
    $a = array("a","b","c");
    $b = array_pop($a);
    return $b;
}

Edit: Both functions you mentioned have the same problem. Assign the array to a variable and pass the variable to array_pop().



回答5:

I think that now (since php 5) it should be:

function &foo(){ //NOTICE THE &
    $b=array_pop(array("a","b","c"));
    return $b;
}
print_r(foo());

and

function &foo(){ //NOTICE THE &
    $a=explode( '/' , 'a/b/c');
    $c=array_pop(array_slice($a, $b = -2, $c = 1)); //NOW NO DIRECT VALUES ARE PASSED IT MUST BE VARIABLES
    return $c;
}
print_r(foo());

but i'm just a begginer :)



回答6:

Try this:

function foo(){
    $a = array("a","b","c");
    $b = array_pop($a);
    return $b;
}


回答7:

Same question Strict Standards: Only variables should be passed by reference, also https://bugs.php.net/bug.php?id=48937.