Modernising Old PHP Code for PHP 8.4 and 8.5 Safely

Modernising Old PHP Code for PHP 8.4 and 8.5 Safely

An application stuck on PHP 7 or early PHP 8 misses security fixes and performance gains, and it gets harder to find developers willing to work on it. Modern tooling makes the upgrade far less risky than it used to be.

1. Build a safety net first

Before changing code, add tests for the paths that matter. Even simple checks that key pages return a 200 status catch a lot.

2. Let PHPStan find problems

composer require --dev phpstan/phpstan rector/rector
vendor/bin/phpstan analyse src --level=3

Begin at a low level and raise it over time. PHPStan reports undefined methods, wrong types and removed functions before users ever see them.

3. Let Rector make the routine changes

// rector.php
use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withPaths([__DIR__ . '/src'])
    ->withPhpSets(php84: true);
vendor/bin/rector process --dry-run
vendor/bin/rector process

Rector updates deprecated syntax across thousands of files in seconds. Review its changes like any other pull request.

4. Update dependencies gradually

Check composer outdated, move frameworks up one major version at a time and replace abandoned packages.

5. Test on both versions

Run your test suite in CI on the old and new PHP versions until you switch, and deploy to staging on the new version before production.

Watch out for

  • Dynamic properties, now deprecated: declare them, or use #[AllowDynamicProperties] as a stopgap.
  • Implicitly nullable parameters such as string $x = null, deprecated in 8.4: write ?string.
  • Stricter comparisons that change how old code behaves.

Need a second pair of eyes on this? Our team handles web development every week. See how we can help.

Keep reading

Related articles

All articles

Need a hand?

Stuck on something similar?
We can fix it with you.

Send us the problem. A senior developer reads every message and replies within one working day.