variable behaviour

This is a discussion on variable behaviour within the PHP Language forums, part of the PHP Programming Forums category; Hello, Could you tell me why a zval cannot be both reference AND value ?? This behaviour leads to this amazing ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008
stef
 
Posts: n/a
Default variable behaviour

Hello,

Could you tell me why a zval cannot be both reference AND value ??

This behaviour leads to this amazing thing :

$var1='Hello';
$var2=$var1; <-- var2 is a "pointer" to var1, sounds normal...


$var3=& $var2 ;

wow... Now $var2 has its own zval memory, It doesn't point anymore to
$var1 !??

I guess, the flag "is_ref" is involved, but I'm not sure...

Anyway, sounds complicated (I mean internally) for a "simple"
assignment.


Thanks...


Reply With Quote
  #2 (permalink)  
Old 04-19-2008
Rik Wasmus
 
Posts: n/a
Default Re: variable behaviour

On Sat, 19 Apr 2008 12:12:44 +0200, stef <stef.pellegrino@gmail.com> wrote:
> Could you tell me why a zval cannot be both reference AND value ??
>
> This behaviour leads to this amazing thing :
>
> $var1='Hello';
> $var2=$var1; <-- var2 is a "pointer" to var1, sounds normal...


Well $var2 is an entirely new value, but untill $var1 or $var2 is changed,
the value isn't copied / doubled as a means of internal PHP optimasation..

> $var3=& $var2 ;
>
> wow... Now $var2 has its own zval memory, It doesn't point anymore to
> $var1 !??


Yes, $var2 is altered in some way. $var3 is a reference to $var2 but NOT
$var1, alterations on $var1 should not effect $var3. In other words:
there's some data in memory available to by $var1. and some data available
to $var2 & $var3. It was probably to difficult (or not worth the trouble)
to keep track of variable names referencing one another and which ones are
'delayed copies, only copied when needed'.

It's an internal PHP thing you shouldn't worry to much about. Possibly it
could be optimised to only really duplicate the original value of $var1 to
$var2 on a change in data, even something references the second one. The
source is freely available, have fun :)
--
Rik Wasmus
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:14 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0