I'll try to put this as simply as possible...
I have a class that contains an array of objects. This class has an addObject() method which simply does the following: -
// array object method
function addObject(&$object) {
$this->_objects[] =& $object;
}
It also has a method to list the object names: -
// array object method to list names
function listNames() {
foreach ($this->_objects as $object) {
echo($object->getName()."<br>");
}
}
Each object is added to the array object when it is created: -
// object constructor
function Object(&$arrayObject, $name) {
$this->name = $name;
$this->arrayObject =& $arrayObject;
$this->_arrayObject->addObject($this);
}
Each object also has a name property with the standard get/set functions: -
function getName() {
return $this->name;
}
function setName($name) {
$this->name = $name;
}
OK... here's the problem. When I run the following code: -
$arr = new $arrayObject();
$obj1 = new Object($arr, 'apples');
$obj2 = new Object($arr, 'bananas');
$arr->listNames();
I get the expected output: -
apples
bananas
But if I run the following code: -
$arr = new $arrayObject();
$obj1 = new Object($arr, 'apples');
$obj2 = new Object($arr, 'bananas');
$obj2->setName('oranges');
$arr->listNames();
I still get: -
apples
bananas
instead of the desired: -
apples
oranges
It seems there's a reference problem here somewhere but I have failed to spot it so far! :-(
Any help is appreciated.