Req #63862 [Opn->Nab]: Passing by reference: func($a = $b);
| From: | krakjoe@php.net | Date: | Wed, 09 Oct 2013 07:20:24 +0000 |
| Subject: | Req #63862 [Opn->Nab]: Passing by reference: func($a = $b); | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-10458@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=63862&edit=1
ID: 63862
Updated by: krakjoe@php.net
Reported by: bobwei9 at hotmail dot com
Summary: Passing by reference: func($a = $b);
-Status: Open
+Status: Not a bug
Type: Feature/Change Request
Package: Documentation problem
Operating System: Mac OS X Mountain Lion
PHP Version: master-Git-2012-12-27 (Git)
Block user comment: N
Private report: N
New Comment:
This is expected behaviour, you cannot use an expression as a reference, you must assign the result
of the expression to a variable and pass it by reference.
Previous Comments:
------------------------------------------------------------------------
[2013-01-05 10:35:17] bobwei9 at hotmail dot com
Yeah, it is primarily not documented, but is there a reason why not to change
behavior? I don't think that it'll introduce a BC break, because it makes today
no sense to write such a code. (Throws E_STRICT error)
I mean: if there is a $variable = on the beginning of an argument and the
argument expects a reference, then evaluate first the part after the first =,
assign the result to the $variable and pas this variable by reference.
Or requires this minor change to be a rfc...?
------------------------------------------------------------------------
[2013-01-05 05:09:45] laruence@php.net
$a = $b result a tmp value, not the left variable self.
I wrote a simple explaination for this before, (however it's in chinese)
http://www.laruence.com/2010/01/03/1225.html
change to doc problem instead
------------------------------------------------------------------------
[2012-12-31 22:52:06] bobwei9 at hotmail dot com
I know how it works now, but I don't know if it is intended (It isn't documented.)
Then I wish that the first variable $a = $b = $c; is passed by array.
------------------------------------------------------------------------
[2012-12-31 22:06:57] mail+php at requinix dot net
FYI:
Expected behavior. The "value" of an assignment is just a value and not a
reference to a variable, just like how "true?$a:$b" is the value of $a and not
actually the variable $a.
I don't think it would be so difficult to change that behavior but I'm afraid of
the consequences it might have.
There's also a more subtle change that would be required for your shorthand to
work. Assignment currently propagates the right-most operand: $a=$b=$c is almost
equivalent* to $b=$c;$a=$c; and not $b=$c;$a=$b; Unless that changes as well then
sort($array=[3,1,2]) would try to sort the [3,1,2] and not $array.
* $c is evaluated only once
------------------------------------------------------------------------
[2012-12-27 16:57:06] bobwei9 at hotmail dot com
Description:
------------
I don't know if this should be considered as a bug or as expected behaviour (=> feature
request).
When passing an assignment to a function which expects a variable as reference like sort(), then an
E_STRICT message is issued and nothing has changed.
What I want, is that a code like "sort($a = $b);" is executed in the same way as "$a
= $b; sort($a);".
This could help to reduce the number of little, unnecessary lines.
Test script:
---------------
php -r 'sort($array = [3,1,2]); var_dump($array);'
Expected result:
----------------
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
}
Actual result:
--------------
Strict Standards: Only variables should be passed by reference in Command line code on line 1
array(3) {
[0]=>
int(3)
[1]=>
int(1)
[2]=>
int(2)
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=63862&edit=1