473,378 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

if $a =& $b is assignment by reference, why don't you need to dereference it?

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?

Sep 28 '07 #1
10 1608
In our last episode, <11*********************@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?
Sep 28 '07 #2
..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
Sep 28 '07 #3
On Sep 27, 11:59 pm, Lars Eighner <use...@larseighner.comwrote:
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.
Sep 28 '07 #4
In our last episode,
<11**********************@o80g2000hse.googlegroups .com>,
the lovely and talented kenneth02394832
broadcast on comp.lang.php:
On Sep 27, 11:59 pm, Lars Eighner <use...@larseighner.comwrote:
>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?
Sep 28 '07 #5
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.
js*******@attglobal.net
==================
Sep 28 '07 #6
kenneth02394832 wrote:
On Sep 27, 11:59 pm, Lars Eighner <use...@larseighner.comwrote:
>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.
js*******@attglobal.net
==================
Sep 28 '07 #7
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
Sep 28 '07 #8
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

Sep 28 '07 #9
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.
js*******@attglobal.net
==================
Sep 28 '07 #10
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/Referen...ter_science%29
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.
js*******@attglobal.net
==================
Sep 28 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Steve | last post by:
what is the difference between passing by & and passing by pointer for example doSomething(int & address); doSomething(int * ptr); thanks
7
by: Computer Whizz | last post by:
Hi, I was just wondering if someone would like to comment on these two issues. I had a 15 minute wander around some sites and was curious about loading files (plain ASCII I think will do for a...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
1
by: jarit | last post by:
Hi, Found these coding guidelines for C#, HTML, Javascript, Java, HTML, PL/SQL, T-SQL, VB and VBScript. Well written and free to download. www.demachina.com/products/swat Jeroen
12
by: Brett Robichaud | last post by:
I need to make access to a reference object threadsafe. My natural instinct was to simply use Monitor.Enter() and Exit(). The problem is that the object behind the reference changes frequently,...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
13
by: Mike S | last post by:
I came across the following paragraph in the "Semantics" section for simple assignment in N1124 (C99 draft) and I'm wondering if I'm interpreting it right: 6.5.16.1p3: If the value being...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
41
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.