Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does extract() work with functions?

extract(some_function_that_returns_array());

works fine.

But on php.net it says that the first paramater must be a referenced variable: http://php.net/manual/en/function.extract.php. Is that a mistake?


so
function foo(&$array){}

function lol(){ $arr = array(); return $arr; }

foo(lol());

shows "Strict Standards: Only variables should be passed by reference in... "

that doesn't happen with extract

like image 652
thelolcat Avatar asked Nov 13 '22 14:11

thelolcat


1 Answers

Either the documentation is in error or the function is in error. Honestly, I can't for any reason see why extract would need to be passed an array by reference, but someone pointed out the EXTR_REFS extract type flag.

Looking at my local verison of the PHP code (5.3.2), I can see that the function definition for extract() does not indicate pass by reference.

function extract (array $var_array, $extract_type = null, $prefix = null)

Looking at sort() the definition does show pass by reference:

function sort (array &$array, $sort_flags = null)

This is why I don't get the strict error on extract() and I see it on sort().

I'm assuming the documentation on php.net is out of date or just plain wrong. extract() is an old puppy... I not sure if way back in the day the function definition actually indicated &$var_array or not.

The other option is the function definition got messed up at some point and the documentation correctly indicates how it should be, based on there being an extra type flag of EXTR_REFS this may be the case (This constant still exists).

like image 121
Ray Avatar answered Nov 15 '22 05:11

Ray