473,320 Members | 1,848 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,320 software developers and data experts.

PHP references

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
Jul 25 '08 #1
1 1244
Taras_96 <ta******@gmail.comwrites:
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.
Jul 25 '08 #2

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

Similar topics

17
by: Tom | last post by:
The motivation for references seems clear: stop people from using nasty pointers when all they really want is a reference to an object. But C++ references are so inadequate that I'm still using...
22
by: xmp333 | last post by:
Hi All, I am trying to hide my JavaScript source. The method I chose was to keep all the important source in a password protected folder, and then use a SRC="folder/script.js" to include it...
33
by: JKop | last post by:
I understand variables/objects and pointer variables perfectly: int X = 5; int* pX = &X; *pX = 4; int** ppX = &pX:
2
by: S. van Beek | last post by:
Dear reader, For removing a reference in the VBA reference form I receive from Doug Steele the following code: ........... References.Remove refCurr
11
by: codebloatation | last post by:
I know how to use references but i DO not get WHY they exist other than to add to the language. Are they actually needed for anything?
14
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will...
30
by: jeremygetsmail | last post by:
I've got an adp (Metrix.adp) with a reference to another adp (InteractSQL.adp). InteractSQL sits on a server, and is refered to by all of the clients (Metrix), which sit on the client machines...
3
by: DonJefe | last post by:
Does anyone have experience using project->project references in large solutions? What are the plus/minuses that you have found? Currently, we are using the binary assembly references for our...
9
by: igor.kulkin | last post by:
References is a relatively basic feature of C++ language. It might be a good thing to think of references as aliases to the variables. However it's good to think of references this way when you...
3
by: CenturionX | last post by:
Hello everybody: I'd like to know what references in my vba code are used or not. I work in a code made by another person previously, i founded to many references and i believe that someones...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.