=& and function(&$param)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I think I'm getting the hang of it. Confusing stuff.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post by TheMoose »

scottayy wrote:I think I'm getting the hang of it. Confusing stuff.
Take it from me, PHP's references are a lot less confusing at first than references in C++. Took me forever to figure out what the hell I needed with pointers pointing to references of other variables.

From my CS class, the teacher told us to think of it like this. Think of variables like a book. Books hold data, variables hold data. People are methods/functions. If I want to give you the data from my book, I give you a copy of my book. It's got the same data right now. But say I write some notes in my book after I gave you a copy, you no longer have the same data I have. If I want you to see the notes I wrote, I have to give you MY book, not just a copy of it. You can write notes in it now, and I will see them. If I write notes, you can see them too. That's a reference to my book.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Forget all about memory for now and just think about behaviour. If you pass an object to a function so that it can modify it, you expect it to modify it right?

Code: Select all

$o = new stdClass();

function addXToObj($obj)
{
  $o->x = 42;
}

addXToObj($o);

var_dump($o); //Empty object
Now with references:

Code: Select all

$o =& new stdClass();

function addXToObj(&$obj)
{
  $o->x = 42;
}

addXToObj($o);

var_dump($o); // object {
  "x" => 42
}
Why? Becuase in the first example $o is copied into the function as $obj, leaving the original $o unchanged. In the second, a reference to the original $o is passed meaning the function actually modifies $o. In OOP you usually expect objects to be passed around in a fashion like this.

arborint makes a good point in that &$obj in PHP4 doesn't provide the same behaviour passing object handles in PHP5.

If you do this in PHP5 you'll get the expected results (like with the & operator in PHP4).

Code: Select all

$o = new stdClass();

function addXToObj($obj)
{
  $o->x = 42;
}

addXToObj($o);

var_dump($o);
But in PHP4, using the reference operator this would destroy the original object, whereas PHP5's passing of object handles wouldn't:

Code: Select all

$o =& new stdClass();

function addXToObj(&$obj)
{
  $o = 42;
}

addXToObj($o);

var_dump($o); // integer { 42 }
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

d11wtq wrote:

Code: Select all

$o =& new stdClass();

function addXToObj(&$obj)
{
  $o = 42;
}

addXToObj($o);

var_dump($o); // integer { 42 }
Did you purposely assign 42 to object $o inside the function, or did you mean to go $o->x = 42; ?
And inside the function, you're using $o instead of the &$obj... how come?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

scottayy wrote:
d11wtq wrote:

Code: Select all

$o =& new stdClass();

function addXToObj(&$obj)
{
  $o = 42;
}

addXToObj($o);

var_dump($o); // integer { 42 }
Did you purposely assign 42 to object $o inside the function, or did you mean to go $o->x = 42; ?
And inside the function, you're using $o instead of the &$obj... how come?
Purposely, although I was meant to write "$obj = 42" not $o. That's why $o comes back out as an integer instead of an object. References are more literal than object handles.

EDIT | My wording is bad/misleading. "More literal" is not correct, they're just different. You're not passing the object by value.
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

VladSun wrote: Thank you for asking this question - it made me read this manual and I found that my understanding of "PHP reference" was very mistaken (i.e. reference in PHP is not like reference in C++). :)
What makes you think so? php references are similar to C++ references (not to confuse with pointers, which are, indeed, something different).
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

stereofrog wrote:
VladSun wrote: Thank you for asking this question - it made me read this manual and I found that my understanding of "PHP reference" was very mistaken (i.e. reference in PHP is not like reference in C++). :)
What makes you think so? php references are similar to C++ references (not to confuse with pointers, which are, indeed, something different).
For example the behaviour of new() in PHP 4.0 ...

But indeed, I agree with you - I really meant that I expected &$var to be a pointer rather than a reference (especially in function arguments)... My mistake.
Last edited by VladSun on Wed Aug 15, 2007 7:46 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

@scottayy - I think that reading about C++ aproach to objects will make things clear. In my opinion OOP PHP is after C++ style...
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply