NYCPHP Meetup

NYPHP.org

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

Hans Zaunere lists at zaunere.com
Wed Jul 20 06:42:46 EDT 2005



On Tuesday, July 19, 2005 10:28 AM, talk-bounces at lists.nyphp.org wrote:
> 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...

That's a good question about the documentation.  There was some, but I'm honestly not sure how current it is, given PHP 5.

http://www.bozemanlug.org/talks/phppresentation.html

And that's not really what I was looking for, but google couldn't come up with the older document when given "andi php references"

But this is new:

http://www.zend.com/php5/andi-book-excerpt.php

> 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?

My understanding is that the entire array will be copied, thus locally scopped, into the function.  When the function returns, the array is removed.  If a "reference" was used, by indicating so with an ampersand, then the original array, outside of the function's scope, would be modified.

The best thing is to test it - make an array with 10mb of junk, then pass it in while watching the size of the PHP process.  If it more than doubles, then it's been copied :)


---
Hans Zaunere
President, Founder
New York PHP
http://www.nyphp.org

AMP Technology
Supporting Apache, MySQL and PHP





More information about the talk mailing list