Link to home
Start Free TrialLog in
Avatar of Xavior2K3
Xavior2K3

asked on

PHP Objects & References

Hi,

I have 2 quick questions about references in PHP, firstly, if i did the following:

     $this->object = new Object();
     $var1 = $this->object;

Would $var1 contain a copy of $this->object, and hence clone it?

Secondly, If I did the following:

     $ref =& $this->object;
     $secondref = $ref;

Does that mean $secondref is a reference to $this->object ($secondref is set to the same memory location as $ref is set to)? Or does PHP treat $ref as the same as $this->object and hence would not contain a reference to it and possibly duplicate it?

Thanks for your help,

Mike
Avatar of hernst42
hernst42
Flag of Germany image

1) depends on you php version in 4 you get a copy in 5 it's the same object

2) $ref is becoming an "alias" for $this->object, but again in php 4 you get a copy, in php5 it's the same object.

See http://www.php.net/manual/en/migration5.oop.php for difference between 4 and 5 in the object model.
Avatar of Xavior2K3
Xavior2K3

ASKER

Sorry, I forgot to mention I'm using PHP 5.

So does this mean these are equivalent?:

     $var1 = $this->object;
     $var1 =& $this->object;

And that after these statements:

     $ref =& $this->object;
     $secondref = $ref;

$this->object, $ref and $secondref all point the the same object?
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial