Of PHP References and Compatibility
October 20th, 2005Following up on the Backward compatibilty and web host adoption of PHP 5 post from two weeks ago, I got a pretty good education about the problem with taking a reference of $this. The best summary is in this bug report.
It comes down to the fact that allowing references to be taken of $this can lead to unexpected values for $this later on. ($this re-assigned) However, it does not appear that a good solution exists:
Given the choice between: (A) doing nothing but leaving room for confusion, (B) taking away the ability to create references, or (C) checking assignments for references to $this(read: slow); The current decision is to go with (A).
The cause of the problems were a result of diverging away from strategy A and a little bit into strategy B. I’m still working on compiling 5.1rc3, so i’m not yet sure how this was actually resolved. I wouldn’t feel bad about a warning when this occurred. However, I think a fatal error is absolutely wrong here. I do feel confident this will get resolved in an acceptable manner because I’m fairly sure that unless it becomes illegal to pass $this to a function by reference that there will always be a workaround for making code compatible with PHP 4 regarding $this. Thanks to everyone who looked in to it.
In the meantime, Derrick Rethans made an article he wrote for php|architect explaining PHP References available. You should read this article. I had to print it to follow it, but I learned something that I did not know. I did not know that PHP uses a copy on write semantic for normal variables. The one question that I wish the article had covered was how objects get automatically passed by reference in PHP 5.
The article debunks some myths about using references for performance. I experimented with this a bit when PHP 4 first came out, but quickly gave up on using references for optimization. I ended up with too many mysterious bugs to track down from “magically connected” variables. I finally ended with some fairly limited rules for using references. I avoid them in all cases except when passing values to functions by reference and for variables that held objects, in which case I use them always. When I want mystery i’ll go to the library, thank you. I don’t need it in my software.
I’ve been looking at the changes necessary for the php 4.4/5.0.5 update. I wrote a little scanner program to help locate suspicious return statements. (I’m far from E_STRICT anyway.) But, I have no programatic help for finding “Only variables can be passed by reference” errors except to actually trigger the error.
I’ve been a little bit frustrated about getting over the compatibility hump with 4.4/5.0.5/5.1rc3. But that seems like nothing compared to the ingrate who emailed Derrick fan mail. This dude needs to get a grip.
I don’t think the update was a mistake at all. Like I said earlier, I’ll take my PHP without mysterious bugs, please. Rather, I think the mistake here was in not understanding the backward compatibility issues. I went back and read some of the orginal discussion about this update. What strikes me was that binary compatibility was discussed, but no-one seemed that excited about the backward compatibility issues at the code level.
So a mistake was made. These things happen. Steps have been taken to ensure that something like this is less likely happen again. Derrick’s fan needs to get over it and move on.
Along these lines, It seems like with so many projects using automated testing that there should be a process for projects with automated test suites to package them up and donate them to an automated backward compatibility lab.
October 21st, 2005 at 1:13 am
Objects are not actually passed by reference in PHP 5. In PHP 5, objects are represented by object handles (a bit like resources) and those handles are passed by value to functions and methods. The engine knows how to de-reference those object handles to refer to the actual object instance. This mechanism is totally separate from the pass-by-ref mechanism.
December 23rd, 2007 at 4:01 pm
Well, php5 objects arent passed by reference as per php 4 definition but they are passed by reference like in Java.