Backporting from PHP 5.3 to 5.2

I have a code base built for PHP 5.3, but due to circumstances (and morons) beyond my control I’ve been forced to backport it to PHP 5.2. There are some nasty surprises not documented in the normal places.

__DIR__

define('APP_ROOT', dirname(__DIR__)); doesn’t work because __DIR__ isn’t defined in 5.2. The documentation┬ásays that __DIR__ is equivalent to dirname(__FILE__) but that’s not quite true. __FILE__ is the path to whichever file contains the magic constant, but __DIR__ is the directory of the running script.

Anonymous functions.

Code like this must be rewritten.

$matches = array();
$note = preg_replace_callback($regex, function ($m) use (&$matches) {
array_push($matches, $m[0]);
return '';
}, $note);

The new/old must use create_function($args, $code), putting all the code into a string so it can be eval()ed later.

.ini files

parse_ini_file($filename) changed the way values with single quotes are handled. In 5.2 parsing a file with key = 'value' returns the value WITH the single quotes. In 5.3 it silently strips the single quotes. This one doesn’t seem to be documented anywhere on php.net.

Of course, this was all discovered while attempting to install the software on a client’s computer. If only the IT department had given them a recent OS. Or given us the ability to update the packages.

Now I wish I had an automated testing system.