Bug #63862 [Opn]: Passing by reference: func($a = $b);

From: Date: Mon, 31 Dec 2012 22:52:06 +0000
Subject: Bug #63862 [Opn]: Passing by reference: func($a = $b);
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-175683@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
 User updated by:    bobwei9 at hotmail dot com
 Reported by:        bobwei9 at hotmail dot com
 Summary:            Passing by reference: func($a = $b);
 Status:             Open
 Type:               Bug
 Package:            Scripting Engine 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:

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.


Previous Comments:
------------------------------------------------------------------------
[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


Thread (3 messages)

« previous php.bugs (#175683) next »