php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #17794 Setting a reference and value on one line
Submitted: 2002-06-16 23:51 UTC Modified: 2004-07-26 17:02 UTC
From: greg at mtechsolutions dot ca Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 4.1.2 OS: *
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: greg at mtechsolutions dot ca
New email:
PHP Version: OS:

 

 [2002-06-16 23:51 UTC] 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.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-06-17 00:35 UTC] mfischer@php.net
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.
 [2004-03-06 21:30 UTC] 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...
 [2004-07-26 17:02 UTC] vrana@php.net
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."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 01:01:28 2024 UTC