A screencast from an empty folder to PSR-4 autoloading with a classmap fallback. PHP has quietly become a pleasant language to write. Typed properties, enums, readonly classes, and now property hooks remove a lot of the boilerplate that gave it a bad reputation.
Sample footage: Spring, an open movie by the Blender Foundation, released under a Creative Commons licence.
Modern PHP, minus the ceremony
We took a class from a six-year-old codebase and rewrote it with current language features. The result is half the length and, more importantly, the invalid states are no longer representable.
- Use readonly properties for value objects.
- Replace string constants with backed enums.
- Commit a PHPStan baseline and shrink it over time.
- Autoload everything through Composer.
Static analysis turns those types into guarantees. Start at a low level with a baseline file, raise the level one step per sprint, and never let the baseline grow.
Questions, corrections, and war stories are welcome in the comments. Several of our best follow-up posts started as a reader pointing out something we had missed.
We will revisit this in six months with fresh numbers. If the results change, we will update the post and note what changed at the end.
If you try this on your own project, start with one page or one service rather than the whole codebase. A small experiment gives you real numbers to argue with, and it is easy to roll back if the idea does not fit.
Programs must be written for people to read, and only incidentally for machines to execute.
Harold Abelson, Structure and Interpretation of Computer Programs, 1985
Clear, practical, no fluff. More of this, please.
It should work without shell access, you just lose the command-line shortcuts.
We tried something similar in 2023 and rolled it back. Curious whether the tooling has caught up since.
Bookmarked. The code sample alone saved me an afternoon.
We tried something similar in 2023 and rolled it back. Curious whether the tooling has caught up since.