Porting from PHP4 Era Code to PHP7

Most of the Changes Were Small, and VIM Macros Helped

Constructors

PHP7 will not call PHP4-style constructors. This will result in weird errors where you get unexpected values.

PHP 4 constructors look like this:

class Foo {
   function Foo(...) {
       ...
   }
}

They need to be replaced with the __construct constructor.:

class Foo {
   function __construct(...) {
       ...
   }
}

ereg_* is Gone

This code should have been changed a decade ago, but it never was.

Simpled regexes can be replace:

ereg('foobar', $text)

Becomes

preg_match('/foobar/', $text)

For more complex regexes, I created functions that were more explicit and did the same thing.

It’s possible to make a macro to make the changes.

cwpreg_match[esc]wwa/[esc]ea'[esc]

Unquoted String Literal Indexes

Back in PHP4, you could do this:

$ar[bingo];

That was equivalent to:

$ar['bingo'];

These will throw warnings in PHP7.

There’s also code that looks like this: array( foo => “bar” );

The foo needs to be quoted.

I needed to quote a bunch of code with this problem. So the macro was pretty simple, but helpful:

a'[esc]wa'[esc]

Adding the Namespace

A lot of errors were related to calling the old DB class. The fix was to use the new DB class.

To enable this, however, I needed the autoloader included on each file. I made a macro to type that in.

I used the Composer autoloader

I figured any replacement classes would be installed by Composer, so I tried to turn the shared
class libraries into a Composer project. Unfortunately, the SSL libraries were whacked out, so
I had problems downloading.

I ended up running composer on a local machine, and uploading the vendor/ directory, which contains
a PSR-4 autoloader.

It wasn’t that bad

Really, it wasn’t that painful. It took around three long days to port la.indymedia.org. I’d guess it’s 80% done,
and I’ll see errors in the logs for a couple more weeks, and can fix things based on error messages.

If anyone needs help with this, I’m interested in doing similar work for pay.

Leave a Reply