ID:               17794
 Updated by:       [EMAIL PROTECTED]
 Reported By:      greg at mtechsolutions dot ca
-Status:           Open
+Status:           Bogus
 Bug Type:         Documentation problem
 Operating System: *
 PHP Version:      4.1.2
 New Comment:

You can make references only to variables, not to expressions. It's
written in the manual page language.references.whatdo: "PHP references
allow you to make two variables to refer to the same content."


Previous Comments:
------------------------------------------------------------------------

[2004-03-06 21:30:28] verdy_p at wanadoo dot fr

I understand the issue that assigning a value to a reference does not
work:
  &$b = 1;
but why doesn't this work:
  &($b = 1);
which assigns the value 1 to a variable but could still maintain the
reference for the rest of the expression where $b should not be
automatically dereferenced.
So we could do:
  $a = &($b[] = new Object());

Work-around:
  $b[] = $a = new Object();
does not work appropriately because $b[] will receive a copy of $a but
not necessarily the same object, so if we then perform:
  $a->field = 1;
then the element in $b[] will not have the added field.
Solution:
  $a =& new Object();
  $b[] =& $a;
The first line assigns to $a the same reference as the new object
instead of copying it.
Then the second line adds the same reference in the array...
My view on the unary & operator is that it is not like the & operator
in C or C++, but instead it's a modifier that instructs to the
following expression that it must not copy the referenced object.
That's why I prefer detaching it from the variable name.
But I still can't understand why the syntax: &(expression) is invalid:
it should simply unflag the implicit dereference of the
right-expression, so that the left-expression is not a a value but can
still be a reference.

This would mean that the following would be valid:
   $a = &1;
meaning that we set $a to the same reference as the reference to the
constant 1, instead of copying that constant into $a... Of course here
the result would be basically the same because such a constant is not
an object by itself. But at least it would have the effect of allowing
autoboxing of primitive types as if they were objects, so it would
unify the datatype model...

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

[2002-06-17 00:35:57] [EMAIL PROTECTED]

You can't assign a value to a reference expression which is exactly
what you try to do:

&$b = 1;

The same applies for the array operator.

Moving it over to a documentation problem so the manual outlines this
better.

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

[2002-06-16 23:51:16] greg at mtechsolutions dot ca

Since you can do:

$a = $b = 1;

It is only natural to assume you can also do:

$a = &$b = 1;

However, this returns a parse error. Should it not return a refernce to
$b ?

Although this (simple) example seems stupid, the reason this is a
problem is when you involve arrays (and in my case, objects):

$a = &$b[] = new Object();

I want to add a new object into $b, but I just want it to go to a new
element number, without having to iterate through (which is currently
the only work-around I can think of).

At the same time, I need a reference to that object stored in another
variable. I can't make a temporary object, as it will be lost (only the
object in the $b array is stored permenantly) and it can't be a copy of
the object, it has to be the specific instance stored in $b.

If this has been fixed in ZE2, please disregard.


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


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

Reply via email to