Bug #53117 [Bgs]: Assignment by reference within assignment operator

From: Date: Thu, 21 Oct 2010 00:06:30 +0000
Subject: Bug #53117 [Bgs]: Assignment by reference within assignment operator
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-155390@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:         cataphract@php.net
 Reported by:        dukeofgaming at gmail dot com
 Summary:            Assignment by reference within assignment operator
 Status:             Bogus
 Type:               Bug
 Package:            *Compile Issues
 Operating System:   Any (Windows 7)
 PHP Version:        5.2.14
 Block user comment: N

 New Comment:

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


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

------------------------------------------------------------------------
[2010-10-20 18:53:06] dukeofgaming at gmail dot com

Description:
------------
Hi,

PHP throws a fatal error when code like this is processed:

$my_var = (true)?(&$some_obj):(&$some_other_obj->some_arr_pop[]);

Which is not a problem if put like this of course:

$my_var;
if(true){
  $my_var = &$some_obj;
}else{
  $my_var = &$some_other_obj->some_arr_pop[];
}

Affects both PHP 5.2.x and 5.3.x



------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=53117&edit=1


Thread (6 messages)

« previous php.bugs (#155390) next »