Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:10349 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27652 invoked by uid 1010); 9 Jun 2004 16:56:19 -0000 Delivered-To: ezmlm-scan-internals@lists.php.net Delivered-To: ezmlm-internals@lists.php.net Received: (qmail 27575 invoked by uid 1007); 9 Jun 2004 16:56:19 -0000 Message-ID: <20040609165619.27561.qmail@pb1.pair.com> To: internals@lists.php.net References: <20040608091715.6553.qmail@pb1.pair.com> <20040608185216.38360.qmail@pb1.pair.com> <20040609072455.27050.qmail@pb1.pair.com> <20040609081333.7480.qmail@pb1.pair.com> Date: Wed, 9 Jun 2004 09:56:38 -0700 Lines: 36 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 X-Posted-By: 64.142.6.231 Subject: Re: Confusing pointers in PHP 4 and 5 From: pollita@php.net ("Sara Golemon") > I think, and I could be completely wrong, that copying a variable actually > creates a reference. The data is only copied when the variable referenced is > modified. > That's true. What I left out of my explanation (in order to keep it simple) is that when you "copy" a variable, a new label is created to point to the same zval, and the zval's refcount is incrmented but the is_ref flag is *not* set (I referred to this offhand as non-reference manner of multiple labels referring to the same value). Then when one of the referring labels says "I want to change my `copy` of the data." It notices that someone else is also referring to this value (in a non-reference manner) and "separates" the zval: This amounts to making a true copy of the zval (with a refcount of 1, and an is_ref of 0) and decrements the refcount of the original zval (since one fewer label is referring to it). This is the process known as "copy on change". $foo = 1; /* $foo (label) ------> 1(value) (is_ref:0 refcount:1) */ $bar = $foo; /* $foo (label) -------> 1(value) */ /* $bar (label) ---/ (is_ref:0 refcount:2) */ $foo = 2; /* $bar (label) -------> 1(value) (is_ref:0 refcount:1) */ /* $foo (label) -------> 2(value) (is_ref:0 refcount:1) */ -Sara Ya just had to make it complicated didn't you.