Automagic migration to PHP 5

I’ve thought about writing something like this for a while now, but so far haven’t had the impetus to do so. However, an upcoming project at work is going to be a PHP 5 migration. Presenting PHP_Migrate4to5, a tool to automate the grunt work of PHP 4 to 5 migration.

What it does

  • Removes var keywords from class variables. Needed for E_STRICT.
  • Adds public/protected/private keywords to class variables and methods. If you use PhpDocumentor, it will use whatever you put in your @access tag. If your functions are prefixed with an underscore, it’s private; public if not. The docblock takes precedence over the function name.
  • Adds static keywords to class variables and methods, if @static is present in the docblock.
  • Renames PHP 4 constructors to __construct(). It also looks for the 4/5 compatibility hack, where you have a 4.x constructor that calls __construct(); in that case, the 4.x constructor is removed.

Future plans

These features may or may not make it in.

  • Locate and fix returning by reference from non-variable
  • Warn about assigning to $this
  • Locate and fix call-time pass-by-reference
  • Better function stripping, including the docblocks. Move docblocks for 4.x constructors to 5.x constructors, if there’s no 5.x docblock.

What it needs

PEAR, PHP_Parser, and some PHP 4.x code. I don’t believe it will work with PHP 5 at the moment, since it has a new token for docblocks. It works fine with PHP 4.3.11.

How to use it

$ php -q Migrate4to5.php Your-PHP4-Code.php > Converted-PHP5-Code.php

Download

Get it here: v0.5.1.

Leave a Reply