RE: [PHP-GENERAL] Assign by reference...

From: Date: Tue, 13 Jun 2000 22:03:47 +0000
Subject: RE: [PHP-GENERAL] Assign by reference...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-1768@lists.php.net to get a copy of this message
Ben, In your C code, you're using pointers to access newly created objects. This is different than de-referencing a variable. The '&' in php seems to me to work like the '&' in C. It doesn't create anything, it just returns the address of the specified variable. It has nothing to do with the actual value of your variable. In your C code you're creating new variables with distinct addresses and values. You're storing the addresses in an array, changing the value, and then calling the value at a particular address. Totally different than using references. Jason > -----Original Message----- > From: Benjamin Berman [mailto:benbfly@yahoo.com] > Sent: Tuesday, June 13, 2000 3:38 PM > To: php-general@lists.php.net; Rasmus Lerdorf > Subject: Re: [PHP-GENERAL] Assign by reference... > > > Rasmus Lerdorf wrote: > > $a=1; > > $b[]=&$a; > > $a=2; > > $b[]=&$a; > > > Rasmus, > > In the case of simple values like ints, I would expect the behavior that > you suggest, namely that re-assigning $a would change the value of > variable at location &$a. But for things like vectors and especially > objects, I expect it to allocate the variable somewhere else in dynamic > memory. This is what I am used to doing in other languages, where > calling "new Obj" gives you a -pointer- to the new object. Look at the > following example in Perl and C++: > > > ------ > Perl > ------ > package Obj; > sub new > { > my ($class) = @_; > my $self = {}; > return bless $self, $class; > } > > $obj = new Obj(); > push(@arr, $obj); > $obj->{a} = 1; > > $obj = new Obj(); > push(@arr, $obj); > $obj->{a} = 2; > > foreach $el (@arr) > { > print $el->{a}."\n"; > } > > > ----- > C++ > ----- > #include <stdio.h> > class Obj > { > public: > int a; > }; > main() > { > Obj* obj; > Obj* new_arr[2]; > int i; > > obj = new Obj(); > new_arr[0] = obj; > obj->a = 1; > > obj = new Obj(); > new_arr[1] = obj; > obj->a = 2; > > for (i=0; i<=1; i++) > { > Obj* obj = new_arr[i]; > printf("%d\n", obj->a); > } > } > > Both of these output "1,2". The same program could be written in java. > It is very natural in all three languages. My question is, can you do > it in PHP4? > > thanks for your help, > ben. > > > Rasmus Lerdorf wrote: > > > > On Mon, 12 Jun 2000, Benjamin Berman wrote: > > > > > I'm having a problem with the new assign by reference feature in PHP4. > > > Consider the following code; it's an artificial example that takes an > > > array of numbers, for instance (1,2,3) and transforms it into an array > > > of arrays, where each array contains multiples of the original number, > > > for instance ((1,10,100), (2,20,200), (3, 30, 300)). Here is > the code: > > > > > > > > > 1 $num_arr = array(1, 2, 3); > > > 2 $multiples_arr = array(); > > > 3 while (list($key, $val) = each($num_arr)) > > > 4 { > > > 5 $mult_arr = array($val, $val*10, $val*100); > > > 6 $multiples_arr[] = &$mult_arr; > > > 7 } > > > 8 > > > 9 while (list($key, $mult_arr) = each($multiples_arr)) > > > 10 { > > > 11 print join(",",$mult_arr)."<BR>\n"; > > > 12 } > > > > > > > > > I would expect to get the output: > > > > > > 1,10,100 > > > 2,20,200 > > > 3,30,300 > > > > > > But instead, I get the following: > > > > > > 3,30,300 > > > 3,30,300 > > > 3,30,300 > > > > > > > > > This is counter-intuitive with respect to other programming languages > > > I've used. I can guess what's happening underneath to make > PHP display > > > this behavior, but it still seems to contradict my notion of what > > > "assignment by reference" means. Is there any way to get PHP > to do what > > > I want it to do here? > > > > I don't understand why you think this is counter-intuitive. To simplify > > it way down, you are basically doing: > > > > $a=1; > > $b[]=&$a; > > $a=2; > > $b[]=&$a; > > > > and you expect the $b array to contain 1,2 > > > > But since all you are doing is putting a pointer to $a into the $b array > > twice, then you cannot possibly have different values in each > slot. That > > is the whole point of references. There is only 1 copy of $a here. > > > > -Rasmus > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > _______________________________________________________ Site Design, Hosting, and E-Commerce at www.webpipe.net

« previous php.general (#1768) next »