Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Upgrading Legacy to the Latest PHP Version

Upgrading Legacy to the Latest PHP Version

Your application runs on an old PHP version that is about to go out of support. How to upgrade without breaking the application? You will need the right tools for detecting and fixing those issues, as well as a way to verify that the application is still working correctly. In this presentation, I will share the strategy that I employed when upgrading millions of lines of untested code.

Avatar for Anna Filina

Anna Filina

June 05, 2025
Tweet

More Decks by Anna Filina

Other Decks in Programming

Transcript

  1. Typical Legacy App • Millions of lines • PHP 4,

    5, 7 • $GLOBALS • Undefines variables • Dead libraries • Removed extensions • No type declarations
  2. • Coding since 1997 • PHP, Java, C#, VB, AS,

    etc. • Principal Engineer @ Zend • Migrating legacy • Writing tests • Teaching developers Anna Filina
  3. Agenda • Show open-source tools • Investigate compatibility issues •

    Tips for fixing issues • Tips for testing a migration
  4. • PHP 7.2 - 7.4: ZendPHP • Backporting security patches

    • Until December 2026 • Can be a pit stop until PHP 8.x Consider Commercial LTS
  5. Report FILE: /www/src/app/controllers/MyController.php ------------------------------------------------------------------ FOUND 1 ERROR AFFECTING 1 LINE

    ------------------------------------------------------------------ 166 | ERROR | Using 'break' outside of a loop or switch structure | | is invalid and will throw a fatal error since PHP | | 7.0 ------------------------------------------------------------------
  6. • Prioritize errors • Research before fixing • Can't know

    values at runtime, such as fopen($path) • Most PHP 8 issues will happen at runtime • PHPStan, Psalm, etc. can find potential runtime issues Using the Report
  7. • php.net – docs, release notes, migration guides • php-legacy-docs.zend.com

    – docs for old PHP versions • phplift.com – building my own migration reference • exakat.readthedocs.io – compatibility rulesets • Experimentation • PHP source code (if obscure, undocumented change) Looking for Answers
  8. Do Your Own Research FILE: /www/src/app/controllers/MyController.php ------------------------------------------------------------------ FOUND 1 WARNING

    AFFECTING 1 LINE ------------------------------------------------------------------ 166 | WARNING | Function each() is deprecated since PHP 7.2; Use | | a foreach loop instead ------------------------------------------------------------------
  9. Returns More Than You Think $key = each($result)['key']; $value =

    each($result)['value']; $key = each($result)[0]; $value = each($result)[1];
  10. function each(array &$array) { if (current($array) === false) { return

    false; } $key = key($array); $value = current($array); next($array); return [ 1 => $value, 'value' => $value, 0 => $key, 'key' => $key, ]; }
  11. • Connection object needs to be tracked. • Connection object

    now a mysqli type. • Field metadata represented differently. • Different methods for unbuffered queries. mysql to mysqli not 1:1
  12. Hidden Global State mysql_connect('localhost', 'user', 'pass'); mysql_query('SELECT * FROM example');

    // vs $connection = mysqli_connect('localhost', 'user', 'pass'); mysqli_query($connection, 'SELECT * FROM example');
  13. • Write tests for legacy • Run tests on legacy

    branch • Upgrade the code • Run same tests on upgrade branch • All tests pass = done Testing Methodology
  14. • More black-box tests, fewer white-box • Discover issues using

    PHPCompatibility • Discover potential issues using PHPStan, Psalm, etc. • Discover the rest based on failed tests • Compare multiple solutions before deciding • Strive for reversible decisions Takeaways
  15. • opencollective.com/phpfoundation • github.com/PHPCompatibility/PHPCompatibility • github.com/phpstan/phpstan • 3v4l.org/sponsor • github.com/phpseclib/mcrypt_compat

    • github.com/jedisct1/libsodium • openssl-foundation.org/donate/ • phpunit.de/sponsors.html Sponsor Your Tools