Doc #53117 [Opn->Asn]: Assignment by reference within assignment operator

From: Date: Fri, 22 Oct 2010 07:34:55 +0000
Subject: Doc #53117 [Opn->Asn]: Assignment by reference within assignment operator
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-5322@lists.php.net to get a copy of this message
Edit report at http://bugs.php.net/bug.php?id=53117&edit=1 ID: 53117 Updated by: aharvey@php.net Reported by: dukeofgaming at gmail dot com Summary: Assignment by reference within assignment operator -Status: Open +Status: Assigned Type: Documentation Problem Package: Documentation problem Operating System: Any (Windows 7) PHP Version: 5.2.14 -Assigned To: +Assigned To: aharvey Block user comment: N Previous Comments: ------------------------------------------------------------------------ [2010-10-21 02:06:30] cataphract@php.net It's not deprecated. What's deprecated is "=& new something()". The documentation page for "Assignment operators" talks briefly about "=&", but it could use some improvements (it even includes the somewhat inaccurate statement that in PHP 5 objects are assigned by reference). ------------------------------------------------------------------------ [2010-10-21 00:57:30] dukeofgaming at gmail dot com Hi again, Thanks for the detailed follow-up. I did not have the slightest idea that this form of using '&' was deprecated. If '=&' is formally an operator, shouldn't it be in the "Assignment Operators" section of the documentation?. Regards ------------------------------------------------------------------------ [2010-10-21 00:39:15] cataphract@php.net > I am not under the impresion that there is an asign-by-reference > operator as such, since one is free to separate "=" from "&", where > "&" indicates return by reference. Not really. See the parser definitions here: http://lxr.php.net/xref/PHP_5_3/Zend/zend_language_parser.y#expr_without_variable The only (non-deprecated) form of an assignment by reference is: variable '=' '&' variable { zend_check_writable_variable(&$1); zend_do_end_variable_parse(&$4, BP_VAR_W, 1 TSRMLS_CC); zend_do_end_variable_parse(&$1, BP_VAR_W, 0 TSRMLS_CC); zend_do_assign_ref(&$$, &$1, &$4 TSRMLS_CC); } The fact you can separate "=" and "&" is maybe unfortunate, because it causes the sort of confusion you have. Although "&" is used in several similar contexts (pass by reference, return by reference, assign by reference, include reference in array expression) and they all share a common implementation pattern (what in PHP is sadly called "references", which is actually just a "no copy flag"), they are still conceptually different processes, with some dissimilarities. In particular, in PHP there are different opcodes for assignment (operator =) and assignment by reference (operator =&). To assign by reference, you don't need just a "reference" (a variable that has the is_ref no copy flag) or something from which we can extract a reference on the right hand side, we need to actually use the "assign by reference" operator. Consider: $a =& $b; $c = $a; //not an assignment by reference, but $a is a "reference". function &h() { return $GLOBALS['b']; } $c = h(); //not an assignment by reference, but h() returns by reference ------------------------------------------------------------------------ [2010-10-20 23:24:28] dukeofgaming at gmail dot com I am not under the impresion that there is an asign-by-reference operator as such, since one is free to separate "=" from "&", where "&" indicates return by reference. However the expression part makes this clear. Regards, David Vega ------------------------------------------------------------------------ [2010-10-20 22:15:12] cataphract@php.net This is not a bug. The ternary operator defines an expression. Even if it allowed yielding a reference (it doesn't), you are not assigning by reference in $my_var = ... The assign by reference operator is "=&", not "=". ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/bug.php?id=53117 -- Edit this bug report at http://bugs.php.net/bug.php?id=53117&edit=1

« previous php.doc.bugs (#5322) next »