if $a =& $b is assignment by reference, why don"t you need to dereference it?
if $a =& $b is assignment by reference, why don"t you need to dereference it?
am 28.09.2007 08:41:40 von Summercoolness
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?
Re: if $a =& $b is assignment by reference, why don"t you need to dereference it?
am 28.09.2007 08:59:10 von Lars Eighner
In our last episode, <1190961700.418756.41530@50g2000hsm.googlegroups.com>,
the lovely and talented Summercool 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
Countdown: 480 days to go.
What do you do when you're debranded?
Re: if $a =& $b is assignment by reference, why don"t you need to dereference it?
am 28.09.2007 10:15:33 von Michael Fesser
..oO(Summercool)
>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
Re: if $a =& $b is assignment by reference, why don"t you need to dereference it?
am 28.09.2007 11:14:38 von kenneth02394832
On Sep 27, 11:59 pm, Lars Eighner wrote:
> In our last episode, <1190961700.418756.41...@50g2000hsm.googlegroups.com>,
> the lovely and talented Summercool 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?
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.
Re: if $a =& $b is assignment by reference, why don"t you need to dereference it?
am 28.09.2007 12:21:00 von Lars Eighner
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 wrote:
>> In our last episode, <1190961700.418756.41...@50g2000hsm.googlegroups.com>,
>> the lovely and talented Summercool 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?
> 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
Countdown: 480 days to go.
What do you do when you're debranded?
Re: if $a =& $b is assignment by reference, why don"t you need todereference it?
am 28.09.2007 13:42:39 von Jerry Stuckle
Summercool 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).
>
Incorrect. C does not have references. It has pointers only. C++ has
references and you can say a=1 - just like you can say $a=1 in PHP.
Just remember in C++ there is a huge difference between
int & ra = b;
// and
int * pa;
pa = &b;
ra is a reference. pa is a pointer. ra is used *exactly* like b - pa
must be dereferenced.
> and i think in the PHP group, people say "reference" to mean "alias".
>
As they do in C++ and Java, amongst other languages.
> So my questions are
>
> 1) If it is by reference, why don't you need to dereference it as
> mentioned above?
>
Because you don't dereference in C++, either.
> 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)
>
They work the same. $a = &$b says to have $a reference $b instead of
making a copy of $b. This is done automatically in PHP 5 for object
(only). But they are the same.
> 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?
>
Again, C does not have references. And PHP works exactly the same as
C++, Java, etc. references.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: if $a =& $b is assignment by reference, why don"t you need to dereference it?
am 28.09.2007 13:43:46 von colin.mckinnon
On 28 Sep, 07:41, Summercool 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.h tml)
>
> 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.
Re: if $a =& $b is assignment by reference, why don"t you need todereference it?
am 28.09.2007 13:44:14 von Jerry Stuckle
kenneth02394832 wrote:
> On Sep 27, 11:59 pm, Lars Eighner wrote:
>> In our last episode, <1190961700.418756.41...@50g2000hsm.googlegroups.com>,
>> the lovely and talented Summercool 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?
>
>
> 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
>
Not if a is a reference to b (or vice versa). True in all languages
which support references.
> 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.
>
>
C++, Java... languages which support references.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: if $a =& $b is assignment by reference, why don"t you need to dereference it?
am 28.09.2007 17:30:57 von Summercoolness
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
Re: if $a =& $b is assignment by reference, why don"t you need to dereference it?
am 28.09.2007 17:43:35 von Summercoolness
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/Reference_%28computer_science%2 9
Re: if $a =& $b is assignment by reference, why don"t you need todereference it?
am 29.09.2007 00:26:56 von Jerry Stuckle
Summercool wrote:
> 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".
>
No. C does not have references. ip is a pointer. Nothing more,
nothing less.
> 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?
>
j is a reference to i.
> 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
>
>
Pointers ARE NOT REFERENCES!
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: if $a =& $b is assignment by reference, why don"t you need todereference it?
am 29.09.2007 00:28:47 von Jerry Stuckle
Summercool wrote:
> 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
>
That is correct. $a is a reference to $b.
> the second behavior is the same as PHP5's object assignment:
> $obj1 = $obj2
>
In PHP5, objects are automatically references. If you want a new copy,
you need a clone() method.
> so it seems like both are called a reference?
>
Yes, the only difference being the first is explicit, the second implicit.
> 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.
>
They are both just references.
>
> some discussions:
> http://en.wikipedia.org/wiki/C%2B%2B_reference
> http://en.wikipedia.org/wiki/Reference_%28computer_science%2 9
>
>
>
I haven't looked at them, but don't believe everything you see in wikipedia.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================