Page 2 of 2

Posted: Wed Aug 15, 2007 11:55 am
by s.dot
I think I'm getting the hang of it. Confusing stuff.

Posted: Wed Aug 15, 2007 12:02 pm
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.

Posted: Wed Aug 15, 2007 12:47 pm
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 }

Posted: Wed Aug 15, 2007 1:11 pm
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?

Posted: Wed Aug 15, 2007 1:25 pm
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.

Posted: Wed Aug 15, 2007 2:18 pm
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).

Posted: Wed Aug 15, 2007 3:36 pm
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.

Posted: Wed Aug 15, 2007 3:45 pm
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...