I've created a short demonstration of the problem I'm having. This isn't exactly how I'm implementing it but seems to lead to the same result.
<?php
class mainclass {
var $vardata = array();
function &__get ($var) {
if ($this->vardata[$var]) return $this->vardata[$var];
if ($var == 'foo') return $this->_loadFoo();
return NULL;
}
function __set ($var, $val) {
$this->vardata[$var] = $val;
}
function __unset($var) {
unset($this->vardata[$var]);
}
}
class extender extends mainclass {
function __construct() {
var_dump($this->foo);
$this->_loadFoo();
echo '<br>';
var_dump($this->foo);
}
function _loadFoo () {
unset($this->foo);
$this->foo = array();
$this->foo[] = 'apples';
$this->foo[] = 'oranges';
$this->foo[] = 'pears';
return $this->foo;
}
}
$test = new extender;
?>
The output of the above code is:
array(3) { [0]=> string(6) "apples" [1]=> string(7) "oranges" [2]=> string(5) "pears" }
array(5) { [0]=> string(6) "apples" [1]=> string(7) "oranges" [2]=> string(5) "pears" [3]=> string(7) "oranges" [4]=> string(5) "pears" }
Where as I was expecting:
array(3) { [0]=> string(6) "apples" [1]=> string(7) "oranges" [2]=> string(5) "pears" }
array(3) { [0]=> string(6) "apples" [1]=> string(7) "oranges" [2]=> string(5) "pears" }
The __get, __set and __unset functions are all being called in the right places and so I'd have expected the second, direct calling of the function to simply unset $this->foo and fill it again with the same data. Meaning that var_dumping it would give the same output. Instead, well, it ends up doing the above... filling it with two of the three strings directly set and keeping the first three originally set strings.
Perhaps just a silly mistake or a misunderstanding of the overloading functions - either way, I've been stuck here for too long now and so any help is much appreciated!