Copy On Write¶
Copy on write
is a data management technic where data is shared between contexts, until is is actually modified. When a modification happens, the data is then duplicated, to avoid polluting the original data. Otherwise, simple reads keep the data intact, and save a copy operation and memory.
PHP uses this technic for arrays and strings, unless they are passed by reference. It is totally transparent for the code.
<?php
$array = [1,2,3];
function foo($a) {
echo $a[1]; //
$a[2] = 4; // $a is copied, then modified.
}
?>