$el = array_shift($instance->find(..))
The above code somehow reports the strict standars 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?
$el = array_shift($instance->find(..))
The above code somehow reports the strict standars 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?
$instance->find()
returns reference to variable.
You get the report when you are trying to use this reference as an argument to function, without storing it at variable first.
This helps preventing memory leaks, and will probably become error in next PHP versions.
Your 2nd code would throw error if it wrote like (note the & in function signature):
function &get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So a quick (and not so nice) fix would be:
$el = array_shift($tmp = $instance->find(..));
Basically you do an assignment to temporary variable first, and send the variable as an argument.
Consider the following code:
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());
This will generate the following output:
Strict Standards: Only variables should be passed by reference in test.php on line 14
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
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);
}
Since you can't change the signature of array_shift
you can also use an intermediate variable:
$inter= get_arr();
$el= array_shift($inter);
Sucks, doesn't it? I don't have an explanation, as I can't read the minds of the PHP internals team and those guys seem to exists on their own wavelength.
The second snippet doesn't work either and that's why. array_shift
is a modifier function, that changes its argument, therefore it expects its parameter to be a reference, and you cannot reference something that is not a variable. See Rasmus' explanations here: http://bugs.php.net/bug.php?id=48937