Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ohffs's avatar
Level 50

Episode/Series Request: Migrating legacy code to Laravel

I admit this is quite a selfish request as I'm working through this process just now**. But it occurred to me that there are quite a few projects out there sitting in a PHP4-lightly-wrapped-in-PHP5 state (if you wrap 3000 lines of procedural code in "class {}" then you're totally doing object orientated programming, am I right?) that people would like to move to a framework like Laravel.

The kind of old scripts that build an html string as they go, run SQL queries directly, are maybe hundreds (or thousands) of lines long, use globals everywhere etc. If you remember the "good old days" (ahem) of...

require_once('includes/globals.inc.php');
require_once('includes/db.inc.php');
$output = "<html><body>\n";
$output = $output . '<table width="100%">' . "\n<tr>\n";
$query = 'SELECT thing FROM table WHERE something="' . mysql_real_escape_string($_GET['name']) . '" LIMIT 1';
$result = mysql_query($query) or die(mysql_error());
$thing = mysql_fetch_array($result);
$output = $output . '<td style="color: red">' . $thing[0] . '</td>' . "\n";
...
(repeat for $n-hundred lines)
...
echo $output;

Happy days.. ;-)

Anyway - as I say, this might just be a selfish request, but it's maybe of wider interest to other PHP folk and getting them on-board Laravel.

** I can give you some truly "remarkable" code if you want to use it as a basis... ;-)

0 likes
11 replies
phildawson's avatar

There's no magic wand for this kind of crappy code. Seems like a pointless exercise translating to a Laravel or any other framework. With code like above its basically starting from scratch anyway. Life is too short for this.

ohffs's avatar
Level 50

@phildawson sadly there's often production sites out there like that which have to keep running. Rewriting from scratch is often not a good or viable option.

olimorris's avatar

Haha @ohffs that's some truly "interesting" code ;-) Reminds me of the good old days c. 2003!

You say "rewriting from scratch is often not a good or viable option" but when code is that bad, how can you not rewrite from scratch. There's the old adage of..."you can't polish a turd"...

A lot of thinking has already been done for you via the legacy app. So you can mimic the way the application functions but using a modern day backend perhaps, such as Laravel. One advantage you have is that you understand your user base (being a site in production for a number of years) and can make reasonable assumptions around scalability and future use etc.

phildawson's avatar

@ohffs

If you are being asked to support and add new functionality to a production site with that code it time to tell the client it needs to be rewritten to continue support or accept the cost/time of rebuild.

Keeping code alive for longer than 3 years is never ideal. Everything has an expiry/support date. It's like having a rust bucket car thats done 250,000 miles and on it's last legs, then trying to start replacing every part bit by bit. Just scrap it and buy an new car.

If you had a starting point of say an old CI site which already had some logic, database and presentation separation then I can see that being possible but having a half-way house between procedural code with a mix of shit like above and Laravel sounds like a nightmare tbh that no dev should have to deal with.

ohffs's avatar
Level 50

@olimorris and that's no-where near as bad as the code I'm actually updating ;-) Scalability isn't much of an issue for the project - but understanding how it functions and what it's supposed to be doing is really, really hard. For one example - there are no parameters passed to any methods - everything is global state. Some variables are actually references set... "somewhere" so even commenting out a line can break (you thought!) un-related classes.

@phildawson to re-write it I'd have to understand what the code is currently doing, which is almost unfathomable. So far I've added the main new feature and reduced the equivalent feature code from c4000 lines of near-untestable Byzantine stuff, to c300 of (reasonably) logical OO testable code. The original code has been built up over the past 10-15 years by a variety of people - most of whom are now long gone. I started out doing a re-write, but eventually it was proving too hard to mimic the behaviour of the current code and make the new stuff interact with the old system (there are python scripts doing stuff in the background, cron jobs doing FTP, remote machines directly manipulating the DB etc etc).

I've been following the process laid out in Modernizing Legacy Applications in PHP with good success so far. To give you an idea of the scale - the original app is >70,000 lines of code and runs production 24x7 for a £100m facility (which is part of the problem - the current code "works" so it's never bothered anyone who's using it).

No-one really knows how it works so part of the process is extracting bits of code just to figure out how things happen. For instance, the first big step was removing all the inline html to templates and making it pass a data-structure for that template - that way I began to get an idea of what the actual logic is (where there was any!). The plan being that once the new feature is in place and working, gradually take that logic and scaffolding and re-write the old code using the same techniques. Once that's done, migrate over to Laravel (and I really, really can't tell you how much I miss it while working on this! ;-)

I'm not suggesting Jeffrey does a series on something remotely this bad, but there's a whole world of old php code out there doing real production work which (I think!) could be brought into the modern world to the benefit all concerned.

phildawson's avatar

there are no parameters passed to any methods - everything is global state. Some variables are actually references set... "somewhere" so even commenting out a line can break (you thought!) un-related classes.

The original code has been built up over the past 10-15 years by a variety of people - most of whom are now long gone.

the original app is >70,000 lines of code and runs production 24x7 for a £100m facility

No-one really knows how it works

I'm stuck with PHP 5.3

Time to switch jobs? :D

ohffs's avatar
Level 50

@phildawson this isn't even my job - I've got to do my original job too while I just, you know, "do that stuff with the website" ;-) I feel so... lucky... ;-)

olimorris's avatar

Oh boy that sounds like a nightmare! I'm surprised the company has not moved to a more integrated IT strategy. Having no support and internal knowledge for a system which is so integral would scare the hell out of me if I was a Director of the company.

@phildawson may have hit the nail on the head though :P

ohffs's avatar
Level 50

@olimorris they are pretty much 100% focussed on physical "plant" equipment (shiny!) and making that work. The back-end IT stuff is something that's a bit of an afterthought, even if it is actually vital to running the place (and anyway, how hard can it be? "I used to make websites using Netscape" etc etc) ;-)

Anyway - I didn't mean this to become a place for me to vent my doom & gloom about this project :-) But I'd be interested in seeing Jeffrey's take on the process one day :-) (Incidentally, if you happen to read this Jeffrey - your lessons on refactoring & testing have been invaluable - thank you!)

Please or to participate in this conversation.