Refactor your PHP code to use descriptive variable names and named constants instead of mystery numbers.
Learn how to keep PHP functions short and single-purpose, avoiding boolean flags and hidden side effects.
Apply the SOLID principles to structure PHP classes for easier future changes.
Use the Bad and Good examples as a code review checklist when reviewing a PHP pull request.
This repository adapts the principles from Robert C. Martin's book "Clean Code" for the PHP programming language. It is not a style guide about formatting or spacing. The focus is on writing PHP code that is easy to read, easy to change, and easy to reuse by yourself or others later. The guide is structured as a series of sections, each covering a different area of code design: how to name variables, how to write functions, how to structure classes, how comparisons work, and how to apply five foundational object-oriented design principles known as SOLID. Within each section, the guide presents concrete before-and-after examples labeled "Bad" and "Good" to show what the recommendation looks like in practice. The naming advice covers things like choosing variable names that are readable aloud, using consistent terminology across similar variables, and replacing mystery numbers with named constants so future readers can tell what the value means. The functions section addresses how to keep functions short, how to avoid passing boolean flags as arguments, and how to prevent a function from silently changing external state. The classes section covers inheritance, composition, and when to mark a class as final so it cannot be extended. The README targets PHP 7.1 and above, though it notes that many developers still use PHP 5. The guide acknowledges that not every principle will suit every project and that reasonable people disagree on some of them. It is presented as a codification of experience rather than a rulebook. The project was inspired by a similar clean-code guide written for JavaScript. Translations into several languages are available in the repository. The full README is longer than what was shown.
← piotrplenik on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.