Your experience Moving PHP 4 to PHP 5

4.5k views Asked by At

We have to move around 50+ Applications (small / large) to PHP 5.3 (from PHP 4.1). Does some has any experience with such an task?

  • Time needed
  • Tools
  • Best setup for environment (Servers/Test?)

Does it make sense to move first to PHP 5.2? Is there any way to automatic detect applications using "PHP 4 Features" wich wont work in PHP 5?

I have no idea how to handle such an project. Thanks!

6

There are 6 answers

2
Spudley On BEST ANSWER
  1. Some of the syntax for classes has changed between PHP4 and PHP5 - for example, in PHP4, the constructor method was named the same as the class, whereas in PHP5, the constructor is named __construct().

    PHP5 can still cope with PHP4-style class definitions, so your code is likely to still work, but you would nevertheless be well advised to change them to the new style, as there are a lot of features that you won't be able to use otherwise. In addition, of course, the old syntax will be removed eventually; your PHP4 classes will break in the future, so better to change them now rather than waiting till it's urgent.

  2. Globals. You should already have been using $_REQUEST, $_POST, $_GET and $_COOKIES in PHP4, a lot of older code may still be using the old style auto-globals that was standard ing PHP3. This is a massive security risk, so if you are still using register_globals, you should start working on your code now to at least use $_REQUEST instead for every place you've used an auto-global. This can actully be a very difficult task -- it can be hard to trawl through a large application trying to work out which variables are intended to be globals and which aren't, when there's nothing in the code to indicate one way or the other. Take it from someone who's had to do this, it can be a real nightmare. But this isn't something specific to moving to PHP5 -- as I said, even if you stick to PHP4, you really do need to deal with this issue. PHP5 doesn't change anything, except that the register_globals flag is now defaulted to being switched off, which may give you a bit more impetus to actually do this work.

  3. If you use any ereg_ regex functions, these have been deprecated. You should replace them with the equivalent preg_ functions. This isn't a big task, and in fact the functions are still available, so it can wait, as long as you're prepared to ignore the warnings telling you the function is deprecated. But again, as with the class syntax, it may be sensible to consider changing them now.

  4. Another feature that has changed, where the old syntax has been deprecated is passing-by-reference. In PHP4, we were encouraged to use the & character in the function call to pass variables by reference. In PHP5, the correct way of doing it is to put the & character in the function declaration rather than where you call it. Again, the old syntax still works, but only if you can put up with PHP throwing warnings at you all over the place.

0
Treffynnon On

If you turn on error_reporting and set it to E_ALL then you should be able to see deprecated errors etc. I wouldn't bother targeting PHP5.2 and then 5.3.

Depends if you just want the apps to work or if you want to take advantage of new PHP features and performance improvements. If they just need to work then only fix the errors and ignore E_DEPRECATED and E_NOTICE warnings.

If the projects were written in a decent way to begin with then it should not be too hard to upgrade them.

That is not to say that it won't be a horribly boring and arduous job. It will also take far longer than you expect; particularly for 50 apps!

0
ZoFreX On

The most important thing to read would be the php.net section on migrating from PHP 4 to PHP 5. Since PHP 5 first came out, they have been moving towards a stricter language (PHP will be in strict mode by default in version 6), so you should expect to see a lot of warnings, if not errors.

There were more backward-compatibility-breaking changes made since PHP 5.0 came out, for completeness you should also read:

Migrating from PHP 5.0.x to PHP 5.1.x

Migrating from PHP 5.1.x to PHP 5.2.x

Migrating from PHP 5.2.x to PHP 5.3.x

I realise that's a lot of reading, but that should be a comprehensive list of everything that could break.

0
Distdev On

First of all you should check php.ini settings, especially such as register_globals, magic_quotes_gpc - it can break the logic of your apps.

0
mario On

PHP_CompatInfo can help with a few checks for dependencies. PHP_CodeSniffer maybe with finding outdated constructs.

However, the differences between PHP versions are often gloryfied. Never had much problems with code relying on fringe cases. Unless of course this code still relies on long superglobals $HTTP_POST_VARS, then a few other gotchas are to be expected.

A helpful idiom to replace magic_quotes dependency for example is:

$_POST = array_map("mysql_real_escape_string", $_POST);

Because realistically most applications are never rewritten to use more contemporary database APIs.

2
StasM On

Turn off display_errors, turn on log_errors, set error_reporting to -1 and look out for E_STRICT and E_DEPRECATED warnings.

This migration script (shameless plug) can help you with some of the deprecated things. It says 5.2 to 5.3 but may be useful for migrating from 4 too. And you can extend it (patches welcome :) for things you discover are frequently encountered.

PHPStorm IDE has pretty decent code analyzer which could point out some of the potential problems in your code. Give it a run through your app and see if it has something interesting to say. Also it will probably help you to find quickly the errors that may follow from using new keywords as function names, etc.

For your last question: IMHO you should go with 5.3, doing the job twice makes little sense.