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

ohffs's avatar
Level 50

Legacy code - advice, hints, tips?

I have been given an old website to add a new feature to with an eye to migrating to Laravel in the longer term.

It's around 70,000 lines of PHP4/early 5 code, no comments to speak of, everything is shared state and global (just newing up a class can modify other classes, add menu entries to the page, update the DB).

Some classes are 4-5000 lines long, there are no parameters passed to any methods - everything reads&writes from a shared global state, require_once's everywhere, there are variables used by reference so you're not quite sure what they are and changing them breaks the whole site (for instance there was a variable '$x' that didn't seem to be used in a method I was looking at - I commented it out to see what happened and every date on every page started returning NULL).

Oh, and no tests, obviously ;-)

So my plan is to add the new feature as separately as I can from the current code using autoloading, small classes, unit tests etc and introduce a small ORM where I can while trying to get a handle on how it hangs together. Once that's done, start going through the old code and refactor it in small chunks (autoloading, breaking up the huge methods, reducing the global state where possible).

The current code is very hard to reason about so writing tests for it is quite difficult as I've really no idea what it should be doing and there are side-effects everywhere - so some avenues are blocked on that front. It also has to work in PHP 5.3 so quite a lot of useful libraries are unavailable to me (as an aside, there are a remarkable number of libraries documented as working with 5.3 which actually use features of 5.4 - fun!)

Anyway - just wondering if anyone else has been here before and could share any tips, strategies etc? I've got a copy of Paul M. Jones 'Modernizing Legacy Applications In PHP' book coming Real Soon Now(TM) which I'm hoping will have some useful pointers, but thought I'd ask you all too.

0 likes
4 replies
Code4Coffee's avatar

In my personal oppinion there is po point in trying rewrite existing code you will need to rewrite it from the ground up to remove all the global states. And integrating laravel into existing codebase will be horror movie and you will end up making compromises to make it work.

I would leave that code alone and setup laravel in a separate folder ( because you know you're going to migrate anyway). Also keeping in mind that once you get rid of legacy code you will be moving laravel to the top of the website.

Then code new features on the laravel and rewrite only when you actually need to touch specific part of the legacy code you can refactor it (or completely rewrite) in the laravel part. In the long run you will reach point where most of the code is based on the laravel.

That's my two cents.

1 like
ohffs's avatar
Level 50

@Code4Coffee thanks for that. I wasn't really planning on integrating laravel into the existing codebase - I'd only migrate to laravel once the codebase was modernised and I could move things over in a sane way.

I'd initially planned to use laravel just for this new feature - but that was before I discovered how intermingled & complex everything is (there are no models, controllers or views in the current code - just spaghetti - it looks like methods were added to whichever file was open at the time rather with any rationale). What I'm hoping to end up with is a bunch of models, a bunch of controllers, helpers etc that I can then sanely reason with and migrate them over to laravel without too much fear.

There is a lot of weird stuff in here - files being scp'd into the codebase, remote bits of software directly manipulating the database under the code, etc. There are cron jobs calling Python jobs which ftp files every minute - every odd, crazy thing you can imagine and the new feature has to work in a somewhat similar way for fear of breaking it all (it's a ~£100m project).

Choices, choices.. ;-)

Francismori7's avatar

£100 m??? Jesus. My two cents, copy over the database to your localhost, and start migrating to Laravel right away, understanding the basics of the application itself, you can go a long way refactoring most of it, and then start moving towards the harder parts, the parts that make no sense, really.

Sorry for your loss buddy. (I meant life by the way)

ohffs's avatar
Level 50

@Francismori7 heh - thanks ;-) Having looked at the db, it only makes life worse - lots of columns like 'x1', 'f2' with zero explanation - other tables that look like they are for lookups, but then are never referenced by the code. Fun times ahead I think.... I'm glad I'm the only developer working on it and have no spec - it all just adds to the .. 'fun' ;-) It's certainly teaching me to make my own code better so the poor schmuck who ever has to fix my old work has a nicer time of it!

Please or to participate in this conversation.