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

From: Date: Tue, 13 Jun 2000 22:37:05 +0000
Subject: RE: [PHP-GENERAL] Assign by reference...
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-1772@lists.php.net to get a copy of this message
Sorry, I guess I messed up but that really didn't have much bearing on my arguement. I think this is basically what you were doing in your C-code example: *char var1; char var2; var2 = 'a'; var1 = &var2; var2 = 'b'; Then *var1 would be 'b' as well. --- But if you did this: char var1; char var2; var2 = 'a'; &var1 = &var2; var2 = 'b'; Then var1 is still equal to 'a' while var2 is equal to 'b'. I believe it was this behaviour that Rasmus was talking about. Jason > -----Original Message----- > From: Benjamin Berman [mailto:benbfly@yahoo.com] > Sent: Tuesday, June 13, 2000 4:15 PM > To: php-general@lists.php.net; venaas@nvg.ntnu.no > Subject: Re: [PHP-GENERAL] Assign by reference... > > > Jason, > > There was a reason that I put the "$obj->a = 1" line -after- the "$arr[] > = $obj" line. This is what assign by reference is all about: when you > change something in the original object, the change is reflected in all > references to the object. You switched around the two lines. Your code > puts a -copy- of the $obj into the array, and then the two copies could > diverge which is not good for OO programming. > > ben. > > > venaas@nvg.ntnu.no wrote: > > > > On Tue, Jun 13, 2000 at 02:38:15PM -0700, Benjamin Berman wrote: > > > 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? > > > > Yes, how about: > > > > class Obj { > > var $a; > > } > > > > $obj = new Obj(); > > $obj->a = 1; > > $arr[] = $obj; > > $obj = new Obj(); > > $obj->a = 2; > > $arr[] = $obj; > > > > while ($el = array_shift($arr)) > > echo $el->a; > > -- > 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 (#1772) next »