The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

PHP5 objects references question

After reading last php|architect article about PHP internal implementation of references i still feel that there's one issue left unspoken. That is PHP5 objects internal model. It's obvious they're not just variables...

How are they implemented actually?

Consider the following lines in PHP5:

$a = new Foo(); $b = $a;

Is it equivalent internally in PHP5 to this:

$a = new Foo(); $b =& $a;

If not what the difference? Is it actually "healthy" to use & with objects in PHP5? Is there speed overhead?

I'm asking all these questions because we're using PHP5 with applications which were written solely for PHP4 yet using OOP. These applications use & almost everywhere and i wonder how PHP5 treats it internally....

P.S. I addressed the same question to the original author(Derrick Rethans) of this article but being a busy guy(i guess) he didn't respond...

The theory is that using & in PHP5 won't make any difference, though as bonefry said, it's "considered deprecated". However, the option remains open to use & to remain consistent with using & with PHP's POD types, and PHP4.

The reality is that & will create no end of problems in PHP5. I've had no end of trouble using it. It make code hard to debug due to the flawed implementation of references in PHP (This applies to 4 and 5). Of course Zend are trying to rectify this, though it breaks a lot of existing code / compatiblity.

Note: There is no performance loss (since PHP 4 and up use reference counting) returning copies instead of references. On the contrary it is most often better to simply work with copies instead of references, because creating references takes some time where creating copies virtually takes no time (unless none of them is a large array or object and one of them gets changed and the other(s) one(s) subsequently, then it would be wise to use references to change them all concurrently).

& is ignored. that's it. There was a speed issue in PHP4 when doing something like $obj1 = $obj2 because a copy was made.

I'm really not sure if & is ignored when working with objects in PHP5. I'm currently away from my dev. box and can't provide any real examples(i'll try to find some tomorrow) but i do remember that we had to use & explicitly when storing objects in session otherwise objects would get corrupted....

Note: There is no performance loss (since PHP 4 and up use reference counting) returning copies instead of references. On the contrary it is most often better to simply work with copies instead of references, because creating references takes some time where creating copies virtually takes no time (unless none of them is a large array or object and one of them gets changed and the other(s) one(s) subsequently, then it would be wise to use references to change them all concurrently).

And you actually believe that ? What proofs do they have (or you) ? How can a object copy, that can hold very much data in it, be faster than a reference ?

Oh I see. They are actually creating references. So if
$a = 5; $b = $a; $c = $b;
value "5" is stored in memory and $a, $b, $c all point to the value. And when one variable is changed, it's actually reasigned. Pretty smart actually. The problem is they wanted to switch to the more common "pass reference by value" used in Java, Python and Ruby to make PHP more object oriented.

Oh I see. They are actually creating references. So if
$a = 5; $b = $a; $c = $b;
value "5" is stored in memory and $a, $b, $c all point to the value. And when one variable is changed, it's actually reasigned. Pretty smart actually. The problem is they wanted to switch to the more common "pass reference by value" used in Java, Python and Ruby to make PHP more object oriented.

Yea, basically it's COW (Copy on Write). This means you can copy an array with many indexes and it will take no time as it is only making references. Only when you change elements will it copy the elements that change. Few people realise this is what happens. That is why you very rarely use references. Generally you only use them on POD types when you want the changes to a variable in a function / method to be reflected in the calling scope.

I'm really not sure if & is ignored when working with objects in PHP5. I'm currently away from my dev. box and can't provide any real examples(i'll try to find some tomorrow) but i do remember that we had to use & explicitly when storing objects in session otherwise objects would get corrupted....

All this PHP reference thing is so much confusing....

In theory & is ignored, or should be ignored for objects. However in practice I find it is not. I've found the use of & in places to be backward compatible with PHP4 has been the source of some very hard to track bugs in PHP due to the nasty reference counting bug I mentioned in the other thread.

Well, if we are to start disbelieving documentation, we should all go find a different line of work. I'm not saying that all documentation is always correct and without errors -- all I want to say is that I will consider a documentation correct (although probably incomplete) until proved differently.