You are here: Re: if $a =& $b is assignment by reference, why don't you need to dereference it? « PHP Programming Language « IT news, forums, messages
Re: if $a =& $b is assignment by reference, why don't you need to dereference it?

Posted by Jerry Stuckle on 09/28/07 11:42

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
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация