Click here to get back home

PHP references

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
PHP references Taras_96 07-25-2008
Posted by Taras_96 on July 25, 2008, 10:50 am
Please log in for more thread options
Hi all

As I understand it, object assignment causes the variable to contain
the same object identifier, whereas assigning a variable via reference
creates a true alias of the RHS:

<?php

class A
{
        public $foo = 1;
}

$a = new A; //<id1>
$b = $a; // <id1> = ($a) = ($b)
$b->foo = 2;
echo "2: ".$a->foo."\n"; // should print out 2
echo "2: ".$b->foo."\n"; // should print out 2

$c = new A; //<id2>
$a = $c; // <id2> = ($a) = ($c), <id1> = ($b)
echo "1: ".$a->foo."\n"; // should print out 1
echo "2: ".$b->foo."\n"; // should print out 2
echo "1: ".$c->foo."\n"; // should print out 1

So far so good, as expected $a = $c causes $a to hold the same
identifier as $c. If $a and $b were TRUE aliases, then echoing $b
would print 1 (which it doesn't).

Now, onto references

Similar to the PHP manual, I group variables in parentheses that are
true aliases of each other

$A = new A; // <id3>
$B = &$A; // ($A,$B) = <id3>
$B->foo = 2;
echo "2: ".$A->foo."\n"; // should print out 2
echo "2: ".$B->foo."\n"; // should print out 2

as expected

echo "\n";

$C = new A;
$A = $C; // ($C) = ($A,$B) = <id3>
echo "1: ".$A->foo."\n"; // should print out 1
echo "1: ".$B->foo."\n"; // should print out 1
echo "1: ".$C->foo."\n"; // should print out 1

now, different to using assignment, changing $A to hold id3 means that
$B will now hold id3, which means that all 3 should print out 1, which
is what happens... still good.

echo "\n";

$D = new A; // <id4>
$A = &$D; // ($A,$B,$D) = <id4>, ($C) = <id3> // shouldn't re-
referncing $A also re-reference $B, as they are aliases of each other?
// actually doing is ($B) = ($C) = <id3>, ($A,$D) = <id4>
$A->foo = 2;
echo "2: ".$A->foo."\n"; // should print out 2
echo "2: ".$B->foo."\n"; // should print out 2
echo "1: ".$C->foo."\n"; // should print out 1
echo "2: ".$D->foo."\n"; // should print out 2

What this actually prints out is 2 1 1 2. Now, when we write $A = &$D,
as $A and $B are aliased, shouldn't they both then point to id4? What
seems to be happening is that $B becomes un-aliased somehow...

Taras

Posted by Chetan on July 25, 2008, 4:18 pm
Please log in for more thread options

> Hi all
>
> As I understand it, object assignment causes the variable to contain
> the same object identifier, whereas assigning a variable via reference
> creates a true alias of the RHS:
>
> <?php
>
> class A
> {
>         public $foo = 1;
> }
>
> $a = new A; //<id1>
> $b = $a; // <id1> = ($a) = ($b)
> $b->foo = 2;
> echo "2: ".$a->foo."\n"; // should print out 2
> echo "2: ".$b->foo."\n"; // should print out 2
>
> $c = new A; //<id2>
> $a = $c; // <id2> = ($a) = ($c), <id1> = ($b)
> echo "1: ".$a->foo."\n"; // should print out 1
> echo "2: ".$b->foo."\n"; // should print out 2
> echo "1: ".$c->foo."\n"; // should print out 1
>
> So far so good, as expected $a = $c causes $a to hold the same
> identifier as $c. If $a and $b were TRUE aliases, then echoing $b
> would print 1 (which it doesn't).
>
> Now, onto references
>
> Similar to the PHP manual, I group variables in parentheses that are
> true aliases of each other
>
> $A = new A; // <id3>
> $B = &$A; // ($A,$B) = <id3>
> $B->foo = 2;
> echo "2: ".$A->foo."\n"; // should print out 2
> echo "2: ".$B->foo."\n"; // should print out 2
>
> as expected
>
> echo "\n";
>
> $C = new A;
> $A = $C; // ($C) = ($A,$B) = <id3>
> echo "1: ".$A->foo."\n"; // should print out 1
> echo "1: ".$B->foo."\n"; // should print out 1
> echo "1: ".$C->foo."\n"; // should print out 1
>
> now, different to using assignment, changing $A to hold id3 means that
> $B will now hold id3, which means that all 3 should print out 1, which
> is what happens... still good.
>
> echo "\n";
>
> $D = new A; // <id4>
> $A = &$D; // ($A,$B,$D) = <id4>, ($C) = <id3> // shouldn't re-
> referncing $A also re-reference $B, as they are aliases of each other?
> // actually doing is ($B) = ($C) = <id3>, ($A,$D) = <id4>
> $A->foo = 2;
> echo "2: ".$A->foo."\n"; // should print out 2
> echo "2: ".$B->foo."\n"; // should print out 2
> echo "1: ".$C->foo."\n"; // should print out 1
> echo "2: ".$D->foo."\n"; // should print out 2
>
> What this actually prints out is 2 1 1 2. Now, when we write $A = &$D,
> as $A and $B are aliased, shouldn't they both then point to id4? What
> seems to be happening is that $B becomes un-aliased somehow...
>
> Taras
References in PHP can be confusing, depending on your notion of what they are
supposed to be. The best way to figure out seemed to be to look at the C source
code. It helps to know this is the way things are.

Similar ThreadsPosted
XML references July 12, 2004, 6:40 am
references April 12, 2007, 5:10 pm
References May 6, 2007, 9:51 am
PHP and returning references September 9, 2004, 5:05 am
References in PHP5 January 19, 2005, 5:03 pm
Question about references July 27, 2005, 3:49 pm
Array references by name August 21, 2005, 8:03 am
Returning References December 31, 2006, 11:04 pm
Arrays and references February 15, 2007, 9:52 am
Arrays always use references? April 12, 2007, 10:53 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap