[SOLVED] PHP4 - referring to objects in foreach loop
Posted: Mon Mar 07, 2005 4:28 am
Can someone explain why if I iterate over an array where the values in the array are objects, referring to the variable that tracks the values does not refer to the original object, but rather refers to a copy?
ie:
this does not update the original objects within $allLords.
However:
Does work.
I have tried adding the ampersand character (for object reference) in front of the key, the array, the value, the assignement, combinations of these. All give an error though.
Is this just the way that PHP4 works, or am I missing something obvious? I suppose that coming from the OO world of Smalltalk, I just assumed that the foreach loop would use a reference to the original array and its objects by default. Mind you, my Smalltalk days are long ago now!
Thanks in advance guys!
ie:
Code: Select all
foreach($allLords as $lordID => $lord){
$lord->setStatus("UPDATED");
}However:
Code: Select all
foreach($allLords as $lordID => $lord){
$allLordsї$lordID]->setStatus("UPDATED");
}I have tried adding the ampersand character (for object reference) in front of the key, the array, the value, the assignement, combinations of these. All give an error though.
Is this just the way that PHP4 works, or am I missing something obvious? I suppose that coming from the OO world of Smalltalk, I just assumed that the foreach loop would use a reference to the original array and its objects by default. Mind you, my Smalltalk days are long ago now!
Thanks in advance guys!