Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Your experience Moving PHP 4 to PHP 5 [closed]

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!

like image 327
opHASnoNAME Avatar asked Feb 04 '11 09:02

opHASnoNAME


4 Answers

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.

like image 66
ZoFreX Avatar answered Nov 20 '22 10:11

ZoFreX


  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.

like image 6
Spudley Avatar answered Nov 20 '22 12:11

Spudley


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.

like image 6
mario Avatar answered Nov 20 '22 10:11

mario


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.

like image 5
Distdev Avatar answered Nov 20 '22 12:11

Distdev