This is a discussion on if $a =& $b is assignment by reference, why don't you need to dereference it? within the PHP Language forums, part of the PHP Programming Forums category; so many places, including the book PHP in a Nutshell, p. 80, it says: $a =& $b # set $a to ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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? |
|
|||
|
In our last episode, <1190961700.418756.41530@50g2000hsm.googlegroups.c om>,
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 <http://larseighner.com/> <http://myspace.com/larseighner> Countdown: 480 days to go. What do you do when you're debranded? |
|
|||
|
..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 |
|
|||
|
On Sep 27, 11:59 pm, Lars Eighner <use...@larseighner.com> wrote:
> In our last episode, <1190961700.418756.41...@50g2000hsm.googlegroups.c om>, > 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. |
|
|||
|
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: >> In our last episode, <1190961700.418756.41...@50g2000hsm.googlegroups.c om>, >> 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 <http://larseighner.com/> <http://myspace.com/larseighner> Countdown: 480 days to go. What do you do when you're debranded? |
|
|||
|
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 ================== |
|
|||
|
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...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. |
|
|||
|
kenneth02394832 wrote:
> On Sep 27, 11:59 pm, Lars Eighner <use...@larseighner.com> wrote: >> In our last episode, <1190961700.418756.41...@50g2000hsm.googlegroups.c om>, >> 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 ================== |
|
|||
|
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 |
|
|||
|
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/Referen...ter_science%29 |