PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hello the forum,
It was fairly horrific figuring out why this was malfunctioning, but I finally understood that the array_push function is pass by value, not by reference. The problem lies in that I am trying to store objects in the array I am pushing to, and subsequent changes to the objects are not reflected if the object is copied. The following code demonstrates:
<?php
class TestObject{
var $value = 0;
function getValue(){
return $this->value;
}
function setValue($mixed){
$this->value = $mixed;
}
}
$myarr = array();
$tmp = new TestObject();
$tmp->setValue(1);
array_push($myarr, $tmp);
$tmp->setValue(2);
$uncertain = array_pop($myarr);
print "value in internal array: ".$uncertain->getValue();
?>
Does anyone know if a pass-by-reference version of array_push exists? I am using version 4.3.3. I know this problem is supposed to be fixed in version 5, but that's unfortunately not an option just yet...