Home > Archive > PHP Programming > September 2007 > if $a =& $b is assignment by reference, why don't you need to dereference it?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
if $a =& $b is assignment by reference, why don't you need to dereference it?
|
|
| Summercool 2007-09-28, 4:03 am |
| 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).
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?
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?
| |
| Lars Eighner 2007-09-28, 4:03 am |
| In our last episode, <1190961700.418756.41530@50g2000hsm.googlegroups.com>,
the lovely and talented Summer broadcast on comp.lang.php:
> 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).
> and i think in the PHP group, people say "reference" to mean "alias".
That is exactly what the manual says in Chapter 21. References Explained.
Instead of guessing, why not RTFM?
--
Lars Eighner <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 480 days to go.
What do you do when you're debranded?
| |
| Michael Fesser 2007-09-28, 4:03 am |
| ..oO(Summer )
>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?
There's nothing to dereference. A reference is not a pointer.
>(don't tell me PHP automatically dereference... as it will be really
>weird).
>
>and i think in the PHP group, people say "reference" to mean "alias".
Exactly. It's just another name for the same value, like a hard link in
a *nix file system.
>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"?
The first is implicitly done by the PHP 5 compiler when working with
objects, the second is explicitly done by the programmer as needed.
Micha
| |
| kenneth02394832 2007-09-28, 8:00 am |
| On Sep 27, 11:59 pm, Lars Eighner <use...@larseighner.com> wrote:
> In our last episode, <1190961700.418756.41...@50g2000hsm.googlegroups.com>,
> the lovely and talented Summer broadcast on comp.lang.php:
>
>
> That is exactly what the manual says in Chapter 21. References Explained.
> Instead of guessing, why not RTFM?
does the manual say why there are two different types of references
and
they behaving differently and they just call it the same name?
in all the languages i know, when you use
a = 0
b = 1
that will break any relationship for variable a and b
no, not for PHP. if there was ever a line
$a =& $b
some where before, then they are alias forever. No other language i
know does that, and then calling it reference to confuse with the
other type of reference.
| |
| Lars Eighner 2007-09-28, 8:00 am |
| In our last episode,
<1190970878.641644.200330@o80g2000hse.googlegroups.com>,
the lovely and talented kenneth02394832
broadcast on comp.lang.php:
> On Sep 27, 11:59 pm, Lars Eighner <use...@larseighner.com> wrote:
[color=darkred]
> does the manual say why there are two different types of references
> and they behaving differently and they just call it the same name?
What two different types of references?
> in all the languages i know, when you use
> a = 0
> b = 1
> that will break any relationship for variable a and b
> no, not for PHP.
Perhaps that is why it is called PHP and not another name.
> if there was ever a line
> $a =& $b
> some where before, then they are alias forever.
see unset.
> No other language i know does that, and then calling it reference to
> confuse with the other type of reference.
It's all a plot, isn't it?
--
Lars Eighner <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 480 days to go.
What do you do when you're debranded?
| |
| C. (http://symcbean.blogspot.com/) 2007-09-28, 8:01 am |
| On 28 Sep, 07:41, Summer <Summer n...@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/0...-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.
| |
| Summercool 2007-09-28, 7:02 pm |
| First of all, I think in the very traditional and basic form of
"reference", it means "pointers".
So that's why in C,
when you say
int *ip;
i is a pointer or "reference" to an integer. and that's why when you
use it to get back the integer, you need to do *ip and that's called
"dereference".
So in C++, it seems that there is a different kind of reference, and
that's like an alias type of reference? So in C++, Java, and PHP, you
can have
int i = 10
int &j = i
printf "%d", j and you get 10?
j = 20
printf "%d %d", i, j and both are 20 now?
that's different from the traditional pointer reference
a = 10; b = 20
int *ip = &a // ip pointers to an integer
int *jp = ip // jp pointers to the same integer
printf "%d", *jp
*jp = 20
printf "%d %d", *ip, *jp
| |
| Summercool 2007-09-28, 7:02 pm |
| sorry, the message was accidentally posted before it was complete:
First of all, I think in the very traditional and basic form of
"reference", it means "pointers".
So that's why in C,
when you say
int *ip;
ip is a pointer or "reference" to an integer. and that's why when you
use it to get back the integer, you need to do *ip and that's called
"dereference".
So in C++, it seems that there is a different kind of reference, and
that's like an alias type of reference? So in C++, Java, and PHP, you
can have
a = 10; b = 20;
int i = a;
int &j = i;
printf "%d", j; and you get 10?
j = b;
printf "%d %d", i, j; and both are 20 now?
that's different from the traditional pointer reference
a = 10; b = 20;
int *ip = &a; // ip pointers to an integer
int *jp = ip; // jp pointers to the same integer
printf "%d", *jp; // print 10
jp = &b; // now jp pointer to a different integer
printf "%d %d", *ip, *jp; // now it prints 10 and 20
the first behavior is the same as PHP's
$a =& $b
the second behavior is the same as PHP5's object assignment:
$obj1 = $obj2
so it seems like both are called a reference?
Isn't there standard names for their difference? The first one is
more like "an alias reference". The second one is like "a pointer
reference". I think calling them the same as "reference" is very
dangerous as they mean different things and behave differently.
some discussions:
http://en.wikipedia.org/wiki/C%2B%2B_reference
http://en.wikipedia.org/wiki/Refere...uter_science%29
|
|
|
|
|