question about references

From: Date: Fri, 02 Aug 2002 13:29:39 +0000
Subject: question about references
Groups: php.general 
Request: Send a blank email to php-general+get-110825@lists.php.net to get a copy of this message
Tried to post this to the news group before, but I'm having trouble getting to my news server from work - hopefully I'm not posting a duplicate. I think I'm seeing the problem I'm seeing because I'm getting a copy of a var instead of a reference to it, but I'm not sure the best we to get around this problem. In my little sample script, I've got an array of objects. When I use foreach to loop through the array and make a change to an item, it doesn't change the object in the array, just the var that I have while I'm in the foreach loop. What's the right way to loop through this array if I really want to change homer's name to marge in this example? The way it is now, I see my echo saying that I'm changing the name, but when I do the second var_dump, it's the same as the first var_dump. Thanks in advance, Jesse <?php class Name { var $firstName; var $lastName; function Name($first, $last) { $this->firstName = $first; $this->lastName = $last; } } $names[] = new Name("joe", "shmo"); $names[] = new Name("billy", "bob"); $names[] = new Name("homer", "simpson"); ?> <html> <body> <?php echo var_dump($names) ?> <br> <?php foreach ($names as $name) { if (strcmp($name->firstName, "homer") == 0) { echo "changing homer to marge<br>"; $name->firstName = "marge"; break; } } ?> <?php echo var_dump($names) ?> <br> </body> </html>

« previous php.general (#110825) next »