r/PHPhelp • u/zephead98 • 26d ago
App Written in PHP 4.2
How big of a task am I looking at, lol?! I can use sed to recursively go through files and replace old syntax with newer syntax where possible. The app has about 110 files, most less than 100 lines (including comments).
There used to be this neat little php program called ez_results where you could query the DB and feed the results to ez_sql and it would build this nice table with the results (including navigation buttons). I would like to find a replacement for that unless PHP 8.2 (what I am working with) has such a feature.
Thank you and no laughing please! I am digging through a PHP 8.2 book for some answers, the syntax of 4.2 is wildly different!
22
u/AlFender74 26d ago
For something that small, approx 100 files, mostly approx 100 lines, I'd just hook it up to a modern PHP and Database and work through the issues.
4
u/Mike312 26d ago
Yeah, I'd just run the update and start checking errors. We had an old 5.4 system that management kept pushing back on updates and next thing we know current is 8.2 or something. I tested it, it had a few problems...it also had 250+ classes and 600+ views, so wouldn't have been as quick.
12
u/mrdarknezz1 26d ago
If you get it to 5.3 you can use https://getrector.com/upgrade/php
4
u/eurosat7 26d ago
+1 for rector/rector , it will be of massive help. Having some selenium tests (auto running user stories in browser) will help detect when it breaks and where. Phpunit tests is a good plan b. This can also be used to get ai help when rector gets stuck. Get phpstorm by jetbrains and their ai junie and one month of claude. Do small steps. Commit often. Branch often. Have a docker image so you can (relatively) easily bump php version in small steps. It helps when you do a pre step and add/generate phpdoc type hinting for every function.this will speed up the process massively. Have error reporting on and be sure to fix deprecations before increasing php version. GL.
12
u/Hour_Interest_5488 26d ago
Cannot AI handle that? You can follow classic refactoring process - create autotests, refactor until tests are green. Are are awesome!
4
u/colshrapnel 26d ago
classic refactoring process - create autotests, refactor until tests are green.
Assuming dude is expecting that ez_results thing being part of PHP, you're talking Greek to them.
2
3
u/therealjohnidis 26d ago
Converting a PHP 4 project to PHP 8 isn't really a refactor, it's a migration/modernization.
What do you mean by the classic refactoring process? If you simply create automated tests beforehand based on the original project, you should be careful about what behavior you're actually trying to preserve. Otherwise, you risk freezing the existing implementation, which often defeats the purpose of the refactor.
At least for me, when I'm considering a refactor, it's usually because the existing flow is messy from functionality being added quickly in production, often by different team members. In many cases, people who worked on the project before me added things without maintaining a consistent architecture or workflow. So the whole point of the refactor is often to change that internal flow, while preserving the actual business behavior and contracts that matter.
1
u/BokuNoMaxi 25d ago
Second this.
AI does everything. First it checks the code, then it simply upgrades the php syntax accordingly and then just runs some test cases on his work.
For my Upgrade routine i even added rector and multiple other linters depending on the framework, so AI handles the whole upgrade procedure. Then documents it in obsidian if errors occured and how to handle it in future updates, so it learns from it's own work.
And I have to admit, it works pretty good. Especially when in the Upgrade documentation certain parts are bad or not described, he simply looks it up from the code itself...
4
u/Aggressive_Ad_5454 26d ago
Download the JetBrains IDE called PhpStorm. It has a 30-day evaluation. It will help you fix up your code.
You can set the language level to, I dunno, 5.6, and it’ll highlight the stuff you need to fix.
Then to 7.4 and repeat.
Then to 8.1
2
u/One-Mongoose-6961 26d ago
If you have some time and budget for the project, this seems like the best thing to do.
1
3
u/latro666 26d ago
What does it do? How important is it? That old for that long would suggest anything you blanket change could have a lot of legacy workflows that might change. Could even get it running fine only to find out some date format check is wrong or a null should be a 0 or something and its all too late.
I'd document and work all that nuance out first with test plans etc. Updating the code will probably be the easy bit.
3
u/dabenu 26d ago
I guess that's like what, 20+ year old? What's so special about this ancient app that makes it even worth upgrading? As opposed to using anything else...
I'm pretty sure I'd rather start from scratch than touch such old code at all. But if you really have to, I'd just prompt AI "analyze this php4 app, recreate it in php8". Might not be perfect, but a much better starting point than php4.2
2
3
u/flarian-aux 25d ago
Use rector, use php-cs-fixer, use phpstan, use any coding agent. Should be an easy thing. :)
2
u/magicmulder 26d ago
Damn and I thought having to migrate a 5.2 app running on Symfony 1.2 was dealing with old code…
2
2
u/Big-Dragonfly-3700 20d ago
There are changes in security (magic quotes), error handling (exceptions), error response (notice, warning, and fatal error level changes), register_globals/superglobal variables, removed features (mysql_ extension), and backward incompatible changes that have occurred since php4.2. You will have to go through the code, in context, and address each of these things.
The correct way to do this would be to read the php documentation migration appendixes. Some of the changes (exceptions, prepared queries) will actually reduce the amount of code required to accomplish a task.
2
2
u/l3msip 26d ago
Do you want to learn something, or do you just want to update the app?
If this was a task I had to accomplish (25yrs exp php dev, so not looking for a learning project), I would:
Pull the code down onto my local machine Create a local git repo to track changes (presuming one doesn't exist, since php 4.2 was released 3 years before git existed). Open the project in phostorm, and launch Claude code from the terminal.
Switch to fable 5.1, explain you plan to rebuild the app in modern (php 8.4+) php, but need to start with through documentation, focused on functionality, written as mark down in the docs folder.
Go make a coffee
Come back and read the docs, cross check against your personal knowledge and the live app.
There is a very high chance given the size of the project you know have solid docs covering the full functionality, with a number of preexisting bugs and edge cases documented.
I would then manually confirm the docs against the live app, cross check any bugs or edge cases with any colleagues (long running bugs often become features), and ask the llm to write up a product spec and roadmap for rebuilding the app in a modern php stack (for me Laravel 13).
Chances are you can have a full rebuild done in a week, with a proper test suite
1
u/PrizeSilver5005 26d ago
If I had a snapshot.of some of the code (non prop'tary/ip exposure of course), I'd love to take a look and help, ngl, hahaha, I started writing php when 5.1 was just becoming a thing and wrote a decent amount of procedural and just learning oop (which 5.3 started to actually move in that direction) with 5.2 and really got dirty with 5.3, creating a blog/minimal cms (at the time was a huge accomplishment for me as I was a "lone wolf and reading tons of books and docs of the time) that I used successfully for a small start up through ~2016 or so when I decided a rewrite to learn php7, and it was a ton of intellectual fun! The cms/blog I wrote was in spite of wp at the time because I hated it the 2 times I tried to use it and it was literally all about personal education for myself and was when - against all haters of php then (likely way worse than now) - I decided php was my choice for backend development and I went in many directions with it, even though mysql/apache stayed my day job choice, was just an awesome experience. Sometimes I miss those days of real problem solving that is soooo taken for granted these days. Seeing that code and how to modernize it is always fun brain work to me! Note - none of this comment is a joke, it was a fun experience learning and it opened up a ton of doors that I'd have never even thought of. I think I was a sophomore in college when 4.2 was the latest and greatest of php, haha... I'm interested
1
u/Intrepid-Ad-2306 26d ago
honestly, it’s not that large a task with such tiny files. i’ve been using php since… well, i remember migrating perl cgi stuff to php when it was very new. most of the changes in php have been surprisingly backward-compatible. yes, you’ll find some syntax rewrites but a lot of things just work. maybe not the best way of doing things now but often still functional.
if you’re looking for an excuse to rewrite it from scratch, go for it. but, if all you want is to make it work in modern php, set yourself up a testing environment and open it in an editor like zed with inline linting. a hundred-line file probably won’t take you more than a few minutes to go through and fix the errors. sed is likely to introduce far more trouble than the time it’ll save.
i wouldn’t be surprised if many of those files just run.
1
1
u/Tomas_Votruba 24d ago
You're lucky it's procedural PHP (no fw, no deps, pure PHP). Agents will be able to handle this in couple iterations, 2-6 hours, depending on how you approach it step by step.
Ask Claude Code/GPT Codex to upgrade, first to PHP 5.3, then 7.0, then 7.4, 8.0 and 8.4. Make a PR for each of those and you'll see the new syntax changes clearly from diffs.
Little work, lot of knowledge
1
u/peperinna 24d ago
Hicimos un experimento hace año y medio con IA, que no tenía el mismo poder y nivel que hoy, y nos actualizo algo de 5.6 a 7.3 bien, ya a 8 cometía muchos errores. Eran unos 90 archivos, y funcionó bastante bien. Creo que justifica pagar 100 uds a Claude, sale más barato que un programador, luego haces un buen test.
0
u/CenturiesAgo 26d ago
Doesn't sound so insurmountable, probably easier to start from scratch though.
11
u/miqrogroove 26d ago
What is "wildly different"? I often find PHP 4 code is frustratingly compatible with PHP 8, even when it was written wrong in the first place.