array_push - pass by reference: does it exist?
Posted: Mon Feb 23, 2004 1:01 am
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:
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...
Any help is appreciated.
AT
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:
Code: Select all
<?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();
?>Any help is appreciated.
AT