Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:51323 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 84740 invoked from network); 19 Jan 2011 17:28:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Jan 2011 17:28:15 -0000 Authentication-Results: pb1.pair.com smtp.mail=glopes@nebm.ist.utl.pt; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=glopes@nebm.ist.utl.pt; sender-id=unknown Received-SPF: error (pb1.pair.com: domain nebm.ist.utl.pt from 193.136.128.21 cause and error) X-PHP-List-Original-Sender: glopes@nebm.ist.utl.pt X-Host-Fingerprint: 193.136.128.21 smtp1.ist.utl.pt Linux 2.6 Received: from [193.136.128.21] ([193.136.128.21:55224] helo=smtp1.ist.utl.pt) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2F/A8-41614-B2F173D4 for ; Wed, 19 Jan 2011 12:28:12 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by smtp1.ist.utl.pt (Postfix) with ESMTP id 7C58F7000449 for ; Wed, 19 Jan 2011 17:28:08 +0000 (WET) X-Virus-Scanned: by amavisd-new-2.6.4 (20090625) (Debian) at ist.utl.pt Received: from smtp1.ist.utl.pt ([127.0.0.1]) by localhost (smtp1.ist.utl.pt [127.0.0.1]) (amavisd-new, port 10025) with LMTP id 1iDGNTkv1FTS for ; Wed, 19 Jan 2011 17:28:08 +0000 (WET) Received: from mail2.ist.utl.pt (mail.ist.utl.pt [IPv6:2001:690:2100:1::8]) by smtp1.ist.utl.pt (Postfix) with ESMTP id 34AF1700044A for ; Wed, 19 Jan 2011 17:28:08 +0000 (WET) Received: from cataphract-old.dulce.lo.geleia.net (cataphract-old.dulce.lo.geleia.net [IPv6:2001:470:94a2:2:9978:6e7:759:40ee]) (Authenticated sender: ist155741) by mail2.ist.utl.pt (Postfix) with ESMTPSA id C7AAD2009007 for ; Wed, 19 Jan 2011 17:28:03 +0000 (WET) Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes To: internals@lists.php.net References: <201101190045.31781.larry@garfieldtech.com> <4D368C58.2040903@yahoo.com.au> <201101190109.52250.larry@garfieldtech.com> <4D369473.4040608@yahoo.com.au> Date: Wed, 19 Jan 2011 17:27:48 -0000 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Organization: =?utf-8?Q?N=C3=BAcleo_de_Eng=2E_Biom=C3=A9di?= =?utf-8?Q?ca_do_IST?= Message-ID: In-Reply-To: User-Agent: Opera Mail/11.00 (Win32) Subject: Re: [PHP-DEV] How deep is copy on write? From: glopes@nebm.ist.utl.pt ("Gustavo Lopes") On Wed, 19 Jan 2011 14:23:49 -0000, Martin Scotta wrote: > What about objects? With objects less copying occurs because the object value (zval) data is actually just a pointer and an id that for most purposes works as a pointer. However, it should be said that while a copy of an array forces more memory to be copied, the inner zvals are not actually copied. In this snippet: $a = array(1, 2, array(3)); $b = $a; function separate(&$dummy) { } separate($a); the copy that occurs when you force the separation of the zval that is shared by $a and $b ($b = $a doesn't copy the array in $a to $b, it merely copies the zval pointer of $a to $b and increments its reference count) is just a shallow copy of hash table and a increment of the first level zvals' refcounts. This means the zvals that have their pointers stored in the array $a's HashTable are not themselves copied. Interestingly (or should I say, unfortunately), this happens even if the inner zvals are references. See http://php.net/manual/en/language.references.whatdo.php the part on arrays. > > class Foo { > public $foo; > } > > function test($o) { > $o->foo->foo->foo = 2; > } > > $bar = new Foo; > $bar->foo = new Foo; > $bar->foo->foo = new Foo; > > test( $bar ); This example shows no copying (in the sense of "new zval allocation on passing or assignment") at all. > > --- > Also... is it better to pass an object as a parameter rather than many > values? > > function withValues($anInteger, $aBool, $aString) { > var_dump($anInteger, $aBool, $aString); > } > > function withObject(ParamOject $o) { > var_dump( $o->theInteger(), $o->theBool(), $o->theString() ); > } > It should be indifferent. In normal circumstances, there is no zval copying at all (only the pointers of arguments' symbols are copied). Only when you start throwing references into the mix will you start forcing copied. -- Gustavo Lopes