Rewrite your legacy PHP apps in... PHP!

PHP has been around for a long time. During it's twenty year tenure of powering the web it has been used for a lot of things. PHP has been and continues to power a lot of ad-hoc, disposable web applications and quick turn around web sites. In addition to this bulk of CMS work and quick hacks, PHP powers a lot of business critical applications. These tend to grow in complexity over time and many of these are 10+ years in age. As a language PHP has also matured along the way. Today it is a perfectly viable option to gradually rewrite your application in modern PHP without the risky big bang rewrite. In fact, PHP is pretty much a perfect tool for this.

Developers love learning new things, but even more than that they love applying them to practise. Management and business folk are usually not that passionate about technologies, infact many loathe the prospect of instability in a software upgrade. At worst either a complete rewrite or stubborn technology stagnation can (and will) run a business down.

If you are working with a large PHP application, you've got a viable option of gradually rewriting your applications. You can even swap out bits not best done in PHP to other tools via messaging queuing systems or direct API calls.

If done right, management does not even need to know about the change in methodology: add new features in a modern way and leave the rest running as is. Let's call it Guerrilla Refactoring.

Tools of the PHP partisan

If you wish to undertake the task of modernising an application, the first thing you'll need to have is a modern PHP version. The newer, the better but PHP 5.3 is the absolute minimum for the practises described to be viable. If you need to, you can use security as leverage.

Once you've upgraded your PHP and sure that the legacy application runs on it, you can start work on improvements. Before work, spend some time evaluating options. Will you start just by importing PHP Components using Composer, or will you tackle legacy using a front controller that will fall back to legacy routes if it's not matched before.

So retain good legacy code by encapsulating it and don't solve problems that have been solved well enough. For the practical execution, you need a strategic level plan for your efforts. There are plenty of valid approaches, here are some resources:

In addition to code level changes, you might want to take the opportunity to refine your toolchain as well. If you're using no version control or SVN, consider adopting Git. But don't make this a priority.

There are plenty of IDEs and other tools that will help you in your modernisation effort. Take a look at these to consider if they would suit your style of working:

Just as with a complete rewrite in PHP (or other languages), it's easy to take too big of a bite. Take on something reasonable first, let it become a routine and then add more as you go. It's still moving forward and rewarding compared to keep on churning on with the same old.

Victorious Freedom Fighters

All of the above is nice theory, but many things remain that. Theory. But PHP is widespread as are the old applications built with this, so this thing is real. And there are examples of it out there. Here are some Content Management Systems built with PHP that have renewed themselves using Symfony.

eZ Publish was gradually written to a Symfony 2 Full Stack application from a PHP application dating back to 1999. This was a large effort, with the added workload from retaining backwards compatibility for current live solutions. This work is now almost complete with the launch of eZ Platform. Read more details from the blog post: Symfony2 for legacy app rejuvenation: the eZPublish case study

Drupal is adopting a lot of Modern PHP practises and code in the upcoming version 8. The latest version promises a lot of technical improvements when it comes to developing templates, swapping components for storage, and what not. Backwards compatibility breaks completely with this release, which has lead to a fork of Drupal 7. Learn more here: Want to be a Drupal 8 Expert? Start with Symfony

Typo3 had a renewal strategy with their own framework, but I find the unofficial effort of integrating Symfony Full Stack Framework with Typo3 more interesting. Bartacus integrates parts of the Symfony 2 Framework to get advantages such as Twig rendering and a good DI container. It uses the old Typo3 as a base structure to give developers familiriaty. Read more: Bartacus Documentation

Other links regarding PHP and it's evolution:

Written by Jani Tarvainen on Sunday September 20, 2015

Permalink - Tags: symfony, php, cms, legacy