Notes on upgrading PHP code from 5.2.x to 5.3.x
The changing face of PHP
As of the 22nd of July, PHP 5.2.x is no longer supported and the latest release 5.2.14 marks the final bug fixing release in the 5.2.x branch. However, if you are like me, you will appreciate that the amount of new features in 5.3 makes this a welcome change as we see new features for 5.3 roll in like namespaces, late static binding and goto functionality, I am happy to see this new face of 5.3 become the standard face of PHP.
However, if you have an existing site, which was written in 5.2.x or before you may be concerned that an upgrade of the PHP on your server will cause issues for your PHP code, and you may be right. Fortunately the amount of deprecated functions from 5.2.x to 5.3 are minimal and even less features have been removed, hence most developers can breathe a sigh of relief.
Tips for updating your code
We will be trying to help you through the upgrade procedure as much as possible, as we roll out 5.3.x to all of our PHP web servers, and of course http://www.php.net can be referenced for the most in-depth discussion of upgrading, we will be trying to focus on the changes most likely to effect an existing site.
For anyone who isn’t even remotely familiar with PHP, it is most likely that you are using a pre-made product, like WordPress, OSCommerce or Joomla, and the best thing to do would be to contact the support/community of whichever product you are using, and ask them for help upgrading your installation to the latest version.
Alternatively, you probably had your site built by a web developer and you will need to contact them, to find out if changing to PHP 5.3 is going to cause you any trouble.
Backwards incompatible changes in PHP 5.3
For the rest of you, you will clearly be looking for some details on what isn’t going to work any more, firstly I want to mention that it is quite unlikely that any of these features are currently in use, but for the sake of completeness I will list them here:
- clearstatcache() no longer clears the realpath cache by default, to make it clear the realpath cache, send a ‘true’ as the first argument to this function.
- realpath() is now fully platform-independent. Consequence of this is that invalid relative paths such as __FILE__ . “/../x” do not work anymore, to correct this, replace __FILE__ with dirname(__FILE__).
- The call_user_func() family of functions now propagate $this even if the callee is a parent class.
- The array functions natsort(), natcasesort(), usort(), uasort(), uksort(), array_flip(), and array_unique() no longer accept objects passed as arguments. To apply these functions to an object, cast the object to an array first, to do this, put ‘natsort((array) $object)’ instead of ‘natsort($object)’.
- The trailing / has been removed from the SplFileInfo class and other related directory classes.
- The __toString magic method can no longer accept arguments.
- The magic methods __get, __set, __isset, __unset, and __call must always be public and can no longer be static. Method signatures are now enforced.
- The __call magic method is now invoked on access to private and protected methods.
- func_get_arg(), func_get_args() and func_num_args() can no longer be called from the outermost scope of a file that has been included by calling include() or require() from within a function in the calling file.
The words ‘goto’ and ‘namespace’ are now reserved.
- call_user_method() (use call_user_func() instead)
- call_user_method_array() (use call_user_func_array() instead)
- ereg() (use preg_match() instead)
- ereg_replace() (use preg_replace() instead)
- eregi() (use preg_match() with the ‘i’ modifier instead)
- eregi_replace() (use preg_replace() with the ‘i’ modifier instead)
- set_magic_quotes_runtime() and its alias, magic_quotes_runtime()
- session_register() (use the $_SESSION superglobal instead)
- session_unregister() (use the $_SESSION superglobal instead)
- session_is_registered() (use the $_SESSION superglobal instead)
- set_socket_blocking() (use stream_set_blocking() instead)
- split() (use preg_split() instead)
- spliti() (use preg_split() with the ‘i’ modifier instead)
- mysql_db_query() (use mysql_select_db() and mysql_query() instead)
- mysql_escape_string() (use mysql_real_escape_string() instead)
- Passing locale category names as strings is now deprecated. Use the LC_* family of constants instead.
- The is_dst parameter to mktime(). Use the new timezone handling functions instead.
So it is clear that upgrading the code from 5.2.x to 5.3.x is not a difficult procedure, and all the new features that will become available in 5.3.x will be very useful in any new projects being built in our hosting accounts. If you have any questions concerning the upgrade of PHP on our servers please contact us at firstname.lastname@example.org