5

Is there any automation tool for converting a php 5.4 script back to 5.3? Mostly there are new style arrays causing problems.

This:

<?php
$x = [1, 2, 3];
$y = [
  'a' => [1,2],
  'b' => 'c',
];
function ff($x = []) { ...}

Should be converted to this:

<?php
$x = array(1, 2, 3);
$y = array(
  'a' => array(1,2),
  'b' => 'c',
);
function ff($x = array()) { ...}
8
  • 2
    I checked around, didn't see anything. Probably can use a perl/sed/awk script to change (\W)\[(.*)\](\W) to \1array (\2)\3, though you would need to handle beginning and ending of line specially with that regex. I suppose you could also write a quick script using PHP 5.4 token_get_all looking for a T_ARRAY ,but that would likely heavily reformat your code.
    – bishop
    Feb 1, 2014 at 16:42
  • 4
    Maybe you could turn around this script: github.com/thomasbachem/php-short-array-syntax-converter
    – dan-lee
    Feb 1, 2014 at 16:43
  • @bishop token_get_all does not treat '[' and ']' as tokens :|
    – hkoosha
    Feb 5, 2014 at 19:16
  • @DanLee I'll try and if it worked I'll post it here.
    – hkoosha
    Feb 5, 2014 at 19:16
  • 1
    Opened a bug report on this.
    – bishop
    Feb 5, 2014 at 20:09

1 Answer 1

3

You can use a tool to convert your codebase from PHP 5.4+ to PHP 5.3, such as: http://github.com/endel/php-code-downgrade/

This tool will read all your files and rewrite code using PHP 5.3 standards, even for your composer dependencies.

The ideal scenario is to write code compatible with your production server, but sometimes it is not possible to know it by antecedence, so this tool may do the job for you.

The advantages of using this is that you can write modern code without worrying about legacy PHP version support.

2
  • 2
    Just want to note the install steps, as seems not yet in github: 1. clone the repository git clone https://github.com/endel/php-code-downgrade.git && cd php-code-downgrade 2. install composer dependencies composer install Jun 2, 2017 at 4:00
  • It should also be made clear that php-code-downgrader converts the files in place, so make sure to run it against a copy of your repo. php php-code-downgrade <path to copy of repo> (or just ./php-code-downgrade <path to copy> if you're on a nix-ish OS and have php in your path).
    – MatsLindh
    May 14, 2020 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.