What this prompt does
This prompt walks the AI through modernizing a specific block of legacy PHP to modern PHP 8.3+ idioms, one technique at a time. Instead of a vague "clean this up," it enumerates eight concrete modernizations — enums, readonly properties and constructor promotion, match expressions, union/intersection and return types, named arguments, the null-safe operator, typed DTOs, and first-class callable syntax — and asks the AI to explain each change and why it improves the code.
The variables keep the refactor safe and reviewable. You paste the code into [code_snippet], set [php_version] as the target feature level, and pin [min_php_version] as the floor you must keep working. That floor matters: it stops the AI from reaching for a syntax your production runtime can't execute. Because the prompt asks for an explanation per change rather than a single rewritten blob, you can review the diff incrementally instead of accepting a risky big-bang rewrite you don't fully understand.
When to use it
- Inheriting an older PHP or Laravel codebase that needs to adopt modern language features
- Replacing class-constant groups with proper enums for type safety
- Tightening a class with readonly properties and constructor promotion
- Converting sprawling switch statements into match expressions
- Adding union, intersection, and return types to untyped legacy methods
- Teaching a team modern PHP by seeing each change explained with its rationale
Example output
You get the refactored version of your [code_snippet], typically broken down change by change: the original construct, the modernized replacement, and a short explanation of why it's better and what edge cases it handles. Enums replace constant groups, switches become match expressions, and array shapes become typed DTOs. The output respects your [min_php_version] floor, so it won't introduce syntax that breaks on your supported runtime, and it reads like a guided review rather than an opaque rewrite.
Pro tips
- Always paste real code into
[code_snippet]; leaving it empty gives you generic examples instead of a refactor of your actual class - Set
[min_php_version]accurately to your lowest production environment — this is the guardrail that prevents unrunnable syntax - Keep snippets focused; refactoring one class or method at a time produces a far more reviewable diff than dumping a whole file
- Push back if the AI applies a feature where it hurts readability — not every switch benefits from becoming a match
- Run your existing tests after each batch of changes; the prompt improves structure but can't guarantee behavior is unchanged
- Use the per-change explanations as review notes, especially when introducing typed DTOs that alter how data moves between layers