|
Posted by C. (http://symcbean.blogspot.com/) on 09/28/07 11:43
On 28 Sep, 07:41, Summercool <Summercooln...@gmail.com> wrote:
> so many places, including the book PHP in a Nutshell, p. 80, it says:
>
> $a =& $b # set $a to reference $b
>
> if $a reference $b, then while you can say $b =1, you can't really
> say $a = 1. you need to say *($a) = 1 as in C or C++.
?
> Referencing
> must be coupled with dereferencing,
?!
> and PHP is not doing the
> dereferencing, so why is it call referencing in the first place?
> (don't tell me PHP automatically dereference... as it will be really
> weird).
It does - when a variable goes out of scope, the reference count on
the thing it references is decremented. If its zero, it gets deleted.
Certainly for very long running applications, referencing dosen't work
for garbage collection - but that should not apply to web pages
(http://symcbean.blogspot.com/2007/09/php-double-plus-good.html)
>
> and i think in the PHP group, people say "reference" to mean "alias".
>
> So my questions are
>
> 1) If it is by reference, why don't you need to dereference it as
> mentioned above?
>
Because dereferencing is always implicit. In the case of objects in
pHP5, the dereference is deferred until a scalar value is required -
which is why object chaining works. When a variable name comes off the
stack, the entity it refers to has its reference count decremented. If
the var name is still in the stack, then the entity is still
required.
> 2) There are actually two "reference" methods (as in my previous post
> topic). One is $obj1 = $obj2, and it works the same as in Java,
> Python, and Ruby (maybe in Perl too?). The other behavior is the $a
> =& $b and it is different, and why is it still called "reference"?
> Why having two different behaviors use the same name which is
> "reference"? Why not call $a =& $b aliasing, and call $obj1 = $obj2
> copying the reference? ($obj2 reference an object, and so copy this
> reference to $obj2)
>
> In a way, if C, C++, Java, Python, Ruby, all use the word reference to
> mean $obj1 = $obj2, if PHP wants to be different and call "aliase" as
> "reference", and different from the rest of the world, I can respect
> that. But the thing is, why call it reference and then have the other
> behavior $obj1 = $obj2 which is different, and AGAIN call it reference?
I've never heard anyone who understands programming languages describe
C pointers as references.
C.
Navigation:
[Reply to this message]
|