NYCPHP Meetup

NYPHP.org

[nycphp-talk] Optimal Object, property, variable passing strategybetween classes

Mikko Rantalainen mikko.rantalainen at peda.net
Tue Jul 19 10:27:37 EDT 2005


Hans Zaunere wrote:
> 
> Exactly.  PHP uses copy-on-write, so even if the ampersand is not used when
> passing variables, the variable's data isn't copied unless it's changed in
> the local scope.

Is there documentation about this somewhere? I'm wondering how this 
works...

Given following code:

function f($a)
{
	$a[2] = "z";
}

$a = array("x","y","z");
f($a);

Will the PHP make a copy of value "z", the whole array $a, or does 
it notice that $a[2] already has the value "z". Or will it create a 
new array $a inside function f() and just uses copy-on-write to the 
values?

I'm pretty sure that PHP doesn't notice that $a[2] isn't used 
anywhere in f() so setting it could be skipped.

-- 
Mikko



More information about the talk mailing list