The PSR-1 naming rules
| Identifier | Convention | Example |
|---|---|---|
| Classes, interfaces, traits | StudlyCaps (MUST) | UserRepository |
| Methods | camelCase (MUST) | findByEmail() |
| Class constants | all upper case with underscores (MUST) | DATE_APPROVED |
| Properties | no rule; be consistent | $userName or $user_name |
StudlyCaps is PSR-1's name for what other guides call PascalCase or UpperCamelCase.
Property names are left to you
PSR-1 deliberately makes no recommendation between $StudlyCaps, $camelCase and $under_score property names. It says only that whatever you choose should be applied consistently within a reasonable scope, such as a vendor, package, class or method.
Namespaces and file names
PSR-1 requires namespaces and classes to follow an autoloading standard, PSR-0 or PSR-4. In practice that means one class per file and a namespace that starts with a vendor name, as in Acme\Billing\Invoice. Under PSR-4 the file is named after the class: Invoice.php.
PSR-1 is a standard, not PHP itself
PHP-FIG is a group of framework and library projects, not the PHP core team, so PSR-1 binds only projects that adopt it. Some frameworks layer their own conventions on top. If you're contributing to an existing project, its style wins.
Converting names with Transform Case
Our snake_case, camelCase, PascalCase and CONSTANT_CASE converters treat an acronym as one word, so HTTP server error becomes HttpServerError and http_server_error. That gives PSR-1 class, method and constant names directly.
Naming conventions in other languages: Python, Java, C#, Go (Golang), Rust, Kotlin, Swift, Dart. All side by side: 20 languages compared.
Sources
- PSR-1: Basic Coding Standard, PHP Framework Interop Group
- PSR-4: Autoloader, PHP Framework Interop Group
Questions
Does PHP have an official naming convention?
Not in the language itself. PSR-1 from the PHP Framework Interop Group is the widely adopted standard: StudlyCaps classes, camelCase methods and upper-case class constants.
Should PHP properties be camelCase or snake_case?
PSR-1 deliberately leaves this open. Choose one and use it consistently across your package.
What is StudlyCaps?
PSR-1's term for PascalCase: every word capitalized and joined with no separator, as in UserRepository.