Daniel Convissor wrote:
> // Copy-on-write is not what happens when dealing only with objects:
> class not_copy_on_write {
> public $m = 'foo';
> }
> $a = new not_copy_on_write;
> $b = $a;
> $a->m = 'bar';
> echo "\$a->m = $a->m, \$b->m = $b->m\n";
>
> ?>
Hi Dan,
Copy-on-write still happens, the example you gave doesn't change a
variable, only a property.
<?php
class copy_on_write {
public $m = 'foo';
}
$a = new not_copy_on_write;
$b = $a; // $a and $b are the same zval, refcount is 2
$a = new not_copy_on_write; // now they are different zvals
$a->m = 'bar';
?>
The above example is equivalent to Sara's simple example.
A better comparison would be between arrays and objects, as this
demonstrates that they behave differently in PHP 5, but were the same in
PHP 4
<?php
$a = array('m' => 'foo');
$b = $a;
$a['m'] = 'bar';
echo \"$a[m] = $a[m], \$b[m] = $b[m]\n";
class not_copy_on_write {
public $m = 'foo';
}
$a = new not_copy_on_write;
$b = $a;
$a->m = 'bar';
echo "\$a->m = $a->m, \$b->m = $b->m\n";
?>
Greg