5.2 to 5.3 migration

January 25, 2010


The new release of PHP 5.3 brought many important additions and changes to PHP. Although longly awaited, some of the changes are not backwards compatible. Thus, code that worked fine with 5.2 may produce warnings or fail in 5.3.

Quick research shows that most prominent applications had to be patched or changed, even though minimally, to work cleanly with 5.3. To help people to convert their web applications to 5.3, I present here a migration script that would allow you to inspect your applications for potential migration problems.
Do not expect supernatural wonders from it, but it may save you some valuable time.

Here's what it can do:

  • catch wrong definitions of __magic methods
  • catch identifier conflicts with newly defined keywords (goto, namespace)
  • catch use of deprecated functions and recommend replacement

Here's what it can't:

  • detect messy use of references
  • detect problems related to changes in function signatures
  • fix the problems for you 😉

The script is very simple in use – just run php migrate.php myscript.php or php migrate.php directory/ and see if you'll get any warnings. If you get none, the script thinks your code is ok :) Running php migrate.php -h displays help.

I have placed the script on github – http://github.com/smalyshev/migrate
If you have any additions or suggestions for the script please comment or contact me directly.

Here’s an example of what it does:

if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
    echo "$regs[3].$regs[2].$regs[1]";
} else {
    echo "Invalid date format: $date";

would produce this warning:

WARNING: Function 'ereg' is deprecated, please use 'preg_match' instead in file test.php line 2

And this code:

class Foo {
    static function __get()
    static protected function __set()

leads to:

WARNING: Magic method __get can not be declared as static in file test.php line 3
WARNING: Magic method __set can not be declared as static in file test.php line 8
WARNING: Magic method __set should be declared as public in file test.php line 8

7 Responses to “5.2 to 5.3 migration”

  1. adkarfa Says:

    <em>Good Man</em>

  2. olleolleolle Says:

    Thank you, Stas, for writing such a useful program.

    I humbly submit an automation for TextMate users, based on the "Validate PHP" command: http://gist.github.com/560530

    Best regards.

  3. texxs Says:

    It actually worked as promised. I’m impressed! your script and advice is pretty helpful for a application we’re working on that we decided to migrate bto 5.3 before it was finished.

  4. ddt Says:

    I forgot that function can be also declared as function &foo(). Now fixed that too.

  5. temporary Says:

    Hmm looks like an ampersand. But your if(!is_array()) fixed it.

  6. ddt Says:

    Тhanks, warnings should be gone now. Could you bring an example of the code where they happened?

  7. temporary Says:

    Just tried it on our code, I’m getting these noticess sometimes:

    PHP Notice: Uninitialized string offset: 1 in /home/swentz/git/migrate/migrate.php on line 228
    PHP Notice: Uninitialized string offset: 1 in /home/swentz/git/migrate/migrate.php on line 230
    PHP Notice: Uninitialized string offset: 1 in /home/swentz/git/migrate/migrate.php on line 234

    Didn’t look deeper into it.