php5 vs. php4 internal object representation
This morning I’ve been thinking about work I’ve done in the past on PHP 4, and some of the projects that I’ve been involved with using PHP 5. The two versions of the php seem miles apart when you’re working with them and have made for some interesting problems. One of the big things that seems like an under the radar change but has overwhelming consequences is changes to the php 5 object model.
For instance, I recall working with some people developing a new feature for their php4 based system, the developer on the project had done all of the initial work on his local box running php5 and figured that they would just upgrade their server to php5 when he was ready to roll out the new feature. However, the company was overly cautious and had put an moratorium on installing php5 on their systems, and the feature had to be dropped onto the php4 system. I was brought in when the developer couldn’t make progress with the system on php4 and was stubbornly trying to get the code to work on php4 because “it worked just fine on php5″. Unfortunately, the developer didn’t understand how deep the changes went in php5 from php4.
If you spend a little bit of time crawling through the php.net documentation website you’ll come upon two entries for classes and objects. Classes and Objects (PHP 4) and Classes and Objects (PHP 5). You can see just from this that there’s a lot of difference between the two versions in terms of objects but what you can’t see in this document is some of the fundamental changes that occurred to make all of this possible.
The largest change in php5 was the move to object representation being done by a handle that refers to the object. This sounds obvious to all of you java, c#, etc..people out there, but in php4 an object was treated as a primitive data type. This resulted in the entire php4 object being copied each time there was an assignment, parameter passing, and returns from functions. This resulted in the programmer spending loads of time trying to wade through the by-reference assignment voodoo.
Now having considered this, let’s go back to our developer porting a php5 application back to php4. He was able to understand all of the syntactic sugar that needed to be modified, and get all of that working pretty easily. However things still didn’t work correctly and he couldn’t wrap his head around the problems.
Do you wanna know what the problem with his application was? Updates to his objects weren’t getting replicated back to the database properly, see this “clone the object” approach of php4 was the basic problem. Once I had him able to see what the real issue was with what he was trying to do, we were able to get things working through a combination of “byref voodoo” and tweaking his object model.
In the end, all things came out relatively well, we were able to get the new features out before the deadline, and things worked swimmingly. It’s interesting to note that if he’d known the details of how php4/php5 worked he wouldn’t have had any problems putting together a slightly modified architecture that would have worked for both without any problems.