Example php code:
class TestArrayObject extends ArrayObject {
function offsetSet($index,$val){
echo $index.':'.$val.PHP_EOL;
}
}
$s = new TestArrayObject();
//test 1
$s['a'] = 'value';//calls offsetSet
//test 2
$s['b']['c'] = 'value';//does not call offsetSet, why?
var_dump($s);
why doesn’t test 2 call the offsetSet method?
ArrayObjectis a big chunk of magic and thus I wouldn’t recommend using it or – even worse – extending it.But your actual problem hasn’t to do with
ArrayObjectby itself. What you are running into is that$s['b']['c'] = ...;is a so called indirect modification of the'b'offset. The code that PHP executes for it looks roughly like this:As you can see offset
'b'is never directly written to. Instead it is fetched by reference and the reference is modified. That’s whyoffsetGetwill be called, notoffsetSet.