I have a question about the copy one write optimization in PHP that I believe is distinct from this and this question marked as a duplicate. I also do not feel the question is addressed here, either this.
In my observation, the copy on write optimization appears to work differently with arrays than with objects. With arrays, it seems that if anything at all changes, no matter how deep in the array, a copy takes place. With objects, it seems that the copy on write only looks at a shallow level.
Consider:
class B{
var $b;
function __construct($x) { $this->b = $x; }
}
$w = [new B(0)];
$x = $w; // $x references $w
$x[0]->b = 1; // now $x does not reference $w
$y = new B(0);
$z = $y; // $z references $y
$z->b = 1; // $z still references $y
I make these confirmations on referencing based on passing $w, $x, $y, and $z into the debug_zval_dump function.
Can someone explain why this is the case? The answer in the second link I provide mentioned "as long as no single byte is changed" and perhaps that is what I need to understand better. My mental model for PHP objects had always been that they are effectively pointers. And the pointer to B in "$x[0]->b = 1" is not changed so I would view that as not a single byte changing.
I also don't see why objects behave differently than arrays in this regard.
Is this behavior properly documented somewhere other than having to read or post on Stack Overflow? The PHP.net manual is quite unhelpful about more theoretical details of the language. With JavaScript, for example, one can view the ECMAScript Standard. That sort of thing doesn't seem to exist for PHP.