I've faced one interesting problem. Creating an array property and assigning a value into it in the same operation using [] seems to go wrong.
class T
{
public function __get($property)
{
if (!isset($this->{$property}))
{
$this->{$property} = array();
}
return $this->{$property};
}
public function __set($key, $val)
{
$this->{$key} = $val;
}
}
$testObj = new T();
$testObj->testArr[] = 1;
$testObj->testArr[] = 2;
$testObj->testArr[] = 3;
var_dump($testObj->testArr);
Outputs the following:
array(2) { [0]=> int(2) [1]=> int(3) }
So 1 is really magically disappeared in the array initialization like a rabbit in the hat. If i try to all $testObj->testArr before the assignment or even assign 1 as $testObj->testArr = array(1); - it works just fine. But i would like to understand the situation and have a universal solution without a need to init the array first. Is there a way to deal with this?