php.net announced the other week that they would no longer be supporting PHP4 past the end of 2007. Any other development for PHP will be done on a case-by-case basis until quarter 3 2008. After that there will be no more updates for PHP4 and it will be classed as discontinued. I have been expecting this announcement since PHP5 was released and PHP6 was announced.
So with PHP4 being stopped there is therefore a real drive to prepare for PHP5 migration, but what should you look out for? The PHP developers have always been keen on making scripts written in one implementation of the language work in the next. So the list of backward incompatibility issues is quite small. Lets have a quick look…
- Keywords – Due to the new (and much better) object orientated model in PHP5 there are a bunch of new keywords. The new list of keywords include exception, final, php_user_filter, interface, implements,public, private, protected, abstract, clone, try, catch and throw.
- strrpos() and strripos() - These two functions find the first instance of a needle in a haystack. The only difference here is that they now take an entire string as a needle, whereas in PHP it must have been one character only.
- Illegal offsets – In PHP4 when you referenced a part of an array that wasn’t part of the array you got a E_WARNING, this has been changed to E_ERROR in PHP5 which will effectively cause the script to stop working if this occurs.
- array_merge() – This function adds arrays together. The difference between PHP4 and PHP5 is that the arguments must now be arrays. An E_WARNING message will be produced for every parameter that is not an array.
- PATH_TRANSLATED – This $_SERVER variable is no longer set by default under Apache2 SAPI. However, this was actually changed in PHP4.3.2 so you have either met it before, or you haven’t realised the change. Either way the use of this $_SERVER variable is quite specialised.
- T_ML_COMMENT – This constant was used in the PHP Tokenizer extension. That is, it was never used at all, and even if your code tried to use it there would be no effect. The constant was defined but never used, and as of PHP5 it has been dropped in favour of using T_COMMENT and T_DOC_COMMENT.
- $_SERVER – This change is quite specific and you should only come across it if you are building your own server and specifically
- Empty objects – When you define an empty object and create it at runtime the object reference will contain an object. In PHP4 it was possible to catch empty objects by using a simple if statement, in PHP5 this will not work in the way intended.
- Class creation – In some instances of object usage in PHP5 (such as using inheritance and interface) you must create all classes before use. This makes sense, how can you extend an object when you don’t know what the parent object looked like?
- get_class(), get_parent_class() and get_class_methods() – These functions are now return case sensitive strings whereas some older implementations of PHP returned all lower case function names. You can compensate for this by making all of the return values lower-case using the strtolower() function.
- ip2long() – This function now returns false if an invalid IP address is supplied as the argument. PHP4 used to return -1.
- Included functions – All functions included in a file are now available. Also, PHP5 will produce a fatal error if you try to include a function that has previously been defined.
- include_once() and require_once() – These functions normalise the path on Windows systems so that oneFile.php and onefile.php are included once.
These changes are few, but most of them will mean very little to you, but tracking down all of these changes might seem like a big job. In my opinion the the best way of checking your site for PHP5 compatibility is to create a test server and see if it runs on that. You can use the PHP function phpinfo() to find out how your server is configured so that you don’t have any nasty shocks. Make sure you turn on error reporting as the default blank screen in your web browser doesn’t mean an awful lot. Creating your own server out of a spare machine in your office is much better than having your server change over and watching your profits plummet as your site is knocked out.
If your host hasn’t moved over to PHP5 yet I would start asking them when they are thinking of doing so, and why they haven’t put any plans in place if they are not. Many applications that run on PHP4 have been fully tested on PHP5. For example, Wordpress has been thoroughly tested on PHP5 and the bugs that were found were actually to do with PHP5 rather than the Wordpress. However, that was with the first initial release of PHP5 and it is now a much more stable, faster and secure scripting platform than it ever has been so there is no reason why you should put it off.
Phil
Programmer, Research and Development