PHP - lost reference in object array? -
i have array stores employee objects ie.
var $this->employeearray = array(); $this->employeearray[] = $empobjecta; $this->employeearray[] = $empobjectb; ... which employee object has id, firstname, lastname etc. have function search employee object id. ie:
public function searcharraybyid($id) { $targetobject = null; foreach($this->employeearray $e) { if ($id == $e->id) { $targetobject = $e; break; } }//foreach return $targetobject; } but when do:
$targetemployee = $this->searcharraybyid(1); $targetemployee->firstname = "someothername"; and
print_r($this->employeearray); that object inside array not being changed.
try this, & prepended, pass reference. simplified search function.
since dont know why isnt working you, because working me on 2 different servers without & can suggest 'safest' method => force references wherever possible
$this->employeearray[] = &$empobjecta; // here public function &searcharraybyid($id) { // here foreach($this->employeearray &$e) { // , here if ($id == $e->id) return $e; } return null; } $targetemployee = $this->searcharraybyid(1); now if doesnt work, suspect error in code, because every reference forced here
funny thing is. tried here: http://phpfiddle.org/main/code/2cv-pt2 , php version, makes no difference (thats how should be). php version using? because php got better @ handling references (reducing unwanted/unnecessary copies)
Comments
Post a Comment