5

The function parse_users returns an array.

I am doing the following in another function:

return reset($this->parse_users($records));

But I get a Strict Standards: Only variables should be passed by reference in...

Is it because I do a reset() on the function?

Do I have to do it this way:

$users = $this->parse_users($records);
return reset($users);

Or is something else?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
PeeHaa
  • 71,436
  • 58
  • 190
  • 262

4 Answers4

5

That's it exactly. reset takes a reference to an array as a parameter, so it basically needs a real variable to reference -- even if it is a pass-by-reference value.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • Which makes obviously sense, because it resets the array-parameter. Otherwise this would not be possible. – hakre Jul 17 '11 at 19:38
  • 2
    @hakre: What doesn't make quite so much sense is that PHP can't get the reference out of the function-expression. – Lightness Races in Orbit Jul 17 '11 at 19:41
  • @Tomalak Geret'kal: Absolutely, right this can be. Was "misguided" by the notice message probably: *"Strict standards: Only variables should be passed by reference"*. So when it comes down to earth, you most probably only want to use it on a variable you've at hand. – hakre Jul 17 '11 at 20:33
3

why didn't you try your

$users = $this->parse_users($records);
return reset($users);

?

It's correct

genesis
  • 50,477
  • 20
  • 96
  • 125
  • It seemed to bloat my code :P Thought perhaps it was the wrong solution / there is a better solution. – PeeHaa Jul 17 '11 at 19:34
3

The one-line-solution uses an additional pair of brackets; this will turn the reference into a variable and omit the error:

return reset( ( $this->parse_users($records) ) );
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
feeela
  • 29,399
  • 7
  • 59
  • 71
  • 2
    Yes, [this works](http://www.ideone.com/SeuOa). I think it'd be hard for this to be widely considered "good practice", since I don't think it's widely known. Off-hand I can't think of a reason why it wouldn't be good practice. – Lightness Races in Orbit Jul 17 '11 at 19:40
  • 2
    [This does not work](http://codepad.viper-7.com/HMuHTu), in fact this will make your code fail with a **[Fatal Error in PHP 5.3](http://codepad.viper-7.com/HMuHTu)**. And seriously, how should the pair of brackets add here really make a difference expression wise? Compile optimization will most certainly drop it. If not now then in the future. – hakre Jul 17 '11 at 19:45
  • @hakre Well, I used that for years and never saw a fatal error. Maybe the codepad.viper-7.com-script is erasing the additional braces, but it works with the [standard PHP-version](http://www.php.net/downloads.php). – feeela Jul 17 '11 at 19:48
  • 2
    @hakre: [Your version with `array()`](http://www.ideone.com/Hxp4C) is **not** the same as [the OP's version with function-call `get_array()`](http://www.ideone.com/SeuOa). `array()` is not a function call. I already demonstrated that it _does_ work for a function-expression. Your argument that optimisation will drop the parentheses is flawed; you've assumed that they are always redundant, with no regard at all for the language's grammar. – Lightness Races in Orbit Jul 17 '11 at 19:49
  • @Tomalak Geret'kal: You're right in the point that array() is not the same as return array(). However the answer was that adding paranthesis is what counts. But if you refer to a function, then [you don't need paranthesis at all.](http://codepad.viper-7.com/vptOgN) – hakre Jul 17 '11 at 19:51
  • 2
    @hakre: anything else then a bare `array()` language construct this works: `function foo(){return array();} reset( ( foo() ) );` (yes, also in PHP 5.3). I'd vote against this solution though, because of it's obscurity which will only serve to baffle future coders on the codebase. Doing a separate fetch & reset will also be just as fast as pipe-lining them. – Wrikken Jul 17 '11 at 19:51
  • @hakre No, I'm using PHP 5.3.5 – feeela Jul 17 '11 at 19:51
  • @hakre: Yes, you do. I posted [a testcase demonstrating that](http://www.ideone.com/liFZu), too. Please start following these links. Your own demonstrations are _different cases_; in particular, you forgot to put `E_STRICT` on (your version with `array()` doesn't require strict mode to fail). – Lightness Races in Orbit Jul 17 '11 at 19:51
  • 2
    @Wrikken: Some people use `-1` trying to mean `E_ALL | E_STRICT`, but [it's flawed](http://www.php.net/manual/en/errorfunc.constants.php) (see comments). – Lightness Races in Orbit Jul 17 '11 at 19:55
  • @Tomalak Geret'kal: While you've opened that can: `-1` is not flawed. It works totally well and there is no comment on the page you've linked that states anything else. But I'm curious what you mean, I think you don't write it just so. – hakre Jul 17 '11 at 19:57
  • 1
    @hakre: Erm, the first two comments list the issues. – Lightness Races in Orbit Jul 17 '11 at 19:58
  • Okay, found my flaw in the script (thanks Wrikken): No Strict standards warning in PHP 5.3 when you put parenthesis around a function call: http://codepad.viper-7.com/AT0fD7 – hakre Jul 17 '11 at 20:04
  • 1
    OK, so we can agree the parentheses work for anything but a bare `array()` language construct? The `-1` is flawed in theory, but suffices for most users in these quick examples, and of course one doesn't use values like those in production (I assume naively...). Back to the matter at hand: _should_ we use these parentheses? Me thinks not as explained earlier, but of course, there are valid reasons to disagree. – Wrikken Jul 17 '11 at 20:10
  • @Wrikken +1 for "it's obscurity" I've searched the web a bit, but can't find the original post, where I get this from years ago. I don't even found it within the PHP-documentation right now… But I used this feature since PHP 4.3 and its still working in 5.3.5. I think it has something to with creating an additional variable scope, while the new created scope is a regular statement in PHP. – feeela Jul 17 '11 at 20:18
  • @Tomalak Geret'kal: Oh so well, let's just assume -1 is worth being ~0. In case not, I switch to the latter. – hakre Jul 17 '11 at 20:26
  • @Wrikken: Yup we can agree. Would be fun to open a bugreport on it, it's not taken care of strict standards. – hakre Jul 17 '11 at 20:27
  • 1
    http://stackoverflow.com/questions/6726589/parentheses-altering-semantics-of-function-call-result – Lightness Races in Orbit Jul 17 '11 at 20:31
1

From the PHP documentation for reset:

mixed reset ( array &$array )

reset() rewinds array's internal pointer to the first element and returns the value of the first array element.

The PHP reset() function accepts a reference to an array. The strict warning is raised because you're directly passing the result of parse_users to reset, without a way to access that array in your other function.

If you're trying to return the full array (and not just the first value) after it's been reset, you should use:

$users = $this->parse_users($records);
reset($users);

return $users;

Alternatively, if you just want the first value from parse_users, you could just use:

$users = $this->parse_users($records);
return $users[0];

The reset function is only needed when you're iterating over the array and want to make sure you start from the beginning.

Bilal
  • 922
  • 6
  • 7