Re: Confusing pointers in PHP 4 and 5
| From: | Aidan Lister | Date: | Wed, 09 Jun 2004 08:13:35 +0000 |
| Subject: | Re: Confusing pointers in PHP 4 and 5 | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-10333@lists.php.net to get a copy of this message | ||
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.
"Bert Slagter" <bert@procurios.nl> wrote in message
news:20040609072455.27050.qmail@pb1.pair.com...
> Sara Golemon wrote:
> > Every "variable" in PHP is a pair.
> >
> > [cut]
> >
> > $foo = 1;
> >
> > /* $foo (label) --------> 1 (value) (is_ref=0, refcount=1) */
> >
> > $bar = &$foo;
> >
> > /* $foo (label) ----------> 1 (value) */
> > /* $bar (label) -------/ is_ref=1, refcount=2 */
> >
> > Hope that helps.
> >
> > -Sara
>
> Thanks for the clear explanation :). I understood that in PHP 5 objects
> are automatically referenced when assigned, and 'primary types' like
> int, bool, string are normally copied when assigned.
>
> When I do a (very rough) benchmark with strings/ints, assigning (and
> thus copying) a 1000 bytes string isn't significantly slower than
> referencing it. Also: memory usage is exactly the same.
>
> Is there any situation in which one might *manually* want to reference a
> variable instead of assigning it (like: $x =& $y)?
>
> Bert