Connecting Tech Pros Worldwide Forums | Help | Site Map

O'Reilly PHP Cookbook in error?

elektrophyte
Guest
 
Posts: n/a
#1: Jul 18 '05
I was looking in _PHP Cookbook_ by David Sklar and Adam Trachtenberg
from O'Reilly. On page 155 under "Assigning Object References" it
says, "Problem: You want to link two objects, so when you update one,
you update the other. Solution: Use =& to assign one object to another
by reference ... When you do an object assignment using =, you create a
new copy of an object ... But when you use =&, the two objects point at
each other."

I thought that when you use =& the thing on the left is a *variable*,
not an object, and that it points to the thing on the right, which is
usually an object. There is no "pointing at each other".

In the PHP manual
(http://www.php.net/manual/en/languag...es.whatdo.php), they go
to great lengths to specifically emphasize this distinction...

<quote>
<?php
$a =& $b;
?>

it means that $a and $b point to the same content.

Note: $a and $b are completely equal here, that's not $a is pointing to
$b or vice versa, that's $a and $b pointing to the same place.
</quote>

Is the book just wrong, or am I not understanding this correctly?

I would be interested to hear others' comments.

Thanks,

E


Colin McKinnon
Guest
 
Posts: n/a
#2: Jul 18 '05

re: O'Reilly PHP Cookbook in error?


elektrophyte wrote:
[color=blue]
> I was looking in _PHP Cookbook_ by David Sklar and Adam Trachtenberg
> from O'Reilly. On page 155 under "Assigning Object References" it
> says, "Problem: You want to link two objects, so when you update one,
> you update the other. Solution: Use =& to assign one object to another
> by reference ... When you do an object assignment using =, you create a
> new copy of an object ... But when you use =&, the two objects point at
> each other."
>[/color]

Yes - this is semantically incorrect.

It would be more correct to say that the two variables reference the same
instance of (the object or variable).

C.

Closed Thread


Similar PHP bytes