5

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

Supericy
  • 5,866
  • 1
  • 21
  • 25
thelolcat
  • 10,995
  • 21
  • 60
  • 102
  • Please consider this a bug and report it. A related bug might be: https://bugs.php.net/bug.php?id=55222 A related question might be: http://stackoverflow.com/q/6726589/367456 – hakre Dec 31 '12 at 18:56
  • well I don't really care about it, but it shows that the manual cannot be trusted :P what if `die()` kills us instead of the script? :( – thelolcat Dec 31 '12 at 18:58
  • lolcatly, take it easy. You can not always trust the docs. Whatever trusting means here btw, I guess this little flaw isn't killing you, right? And if you don't care, then please don't ask. – hakre Dec 31 '12 at 18:59
  • @thelolcat I've personally found a handful of documentation bugs related to function signatures. They're not too uncommon. – goat Dec 31 '12 at 19:00
  • 2
    If you think something in the docs can be improved everybody is encouraged to fix it: https://edit.php.net/?project=PHP. – PeeHaa Dec 31 '12 at 19:02
  • it asks me for my email password – thelolcat Dec 31 '12 at 19:02
  • You can login anonymously :) @thelolcat – PeeHaa Dec 31 '12 at 19:04
  • perhaps you should put the whole warning message instead of an ellipsis? – didierc Dec 31 '12 at 19:05
  • thanks, but it seems that the function definition is not editable. @diderc: i can't bc it shows the location of my computer and then u can hack me and stuff – thelolcat Dec 31 '12 at 19:07
  • @thelolcat It is in ``. – PeeHaa Dec 31 '12 at 19:08
  • i dont think anyone should edit the php docs. i remember reading something by rasmus about why the docs specify a reference arg, yet its only enforced in certain cases. – goat Dec 31 '12 at 19:18
  • 1
    here's something relevant https://bugs.php.net/bug.php?id=55754 (it explains a zend engine flag - and shows us there's a "prefer reference" and "require reference" distinction made by the zend engine) – goat Dec 31 '12 at 19:25
  • @thelolcat see my update, extract just hasn't been updated in the documentation. – Ray Dec 31 '12 at 19:34

1 Answers1

1

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).

Ray
  • 40,256
  • 21
  • 101
  • 138
  • 1
    This is incorrect. Just because call-time pass-by-reference was deprecated in 5.3 and removed in 5.4 does not mean that you can no longer pass vars by reference. Completely unrelated to OP's question. – igorw Dec 31 '12 at 18:56
  • @igorw this is correct. Passing by reference is achieved by the definition of the function, not specified when you call the function. – Ray Dec 31 '12 at 19:00
  • 1
    This answer has nothing to do with the question. OP is not asking about call-time at all. – webbiedave Dec 31 '12 at 19:08
  • @Ray, a proper answer to this question can be used to explain the difference in behavior between `extract(lol())` and `sort(lol())` - the difference being that only one of them triggers a php notice – goat Dec 31 '12 at 19:17
  • @iputonmyrobeanwizardhat ok, I updated my answer. The question changed from his original post. – Ray Dec 31 '12 at 19:32
  • @igorw my changes now address the issue – Ray Dec 31 '12 at 19:35
  • @webbiedave I read quickly an earlier version of the question. Based on the edits to the question I get what's being asked and have updated my answer. – Ray Dec 31 '12 at 19:36
  • The reason it takes a reference is probably because of the `EXTR_REFS` option. Kind of like if you want to retain the reference when setting a member using a method (`public function x(&$ref) { $this->ref =& $ref; }`). – Sverri M. Olsen Dec 31 '12 at 19:40
  • @SverriM.Olsen Hmm... this would indicate a bug in the function definition then if EXTR_REFS is still supported. – Ray Dec 31 '12 at 19:45
  • @Ray What do you mean by "still supported"? `EXTR_REFS` was added in 4.3.0 and has been there ever since. – Sverri M. Olsen Dec 31 '12 at 22:00