dev-resources.site
for different kinds of informations.
PSR-1: Basic Coding Standard in PHP
Published at
1/6/2025
Categories
php
standards
phpfig
Author
jonesrussell
Author
12 person written this
jonesrussell
open
Ever wondered why some PHP codebases are a joy to work with while others feel like a maze? A lot of it comes down to following consistent coding standards. Let’s explore PSR-1, the foundation of modern PHP development that helps teams write cleaner, more maintainable code!
Overview of PSR-1 Rules
1. Files and Namespaces
- Files MUST use only
<?php
and<?=
tags - Files MUST use only UTF-8 without BOM for PHP code
- Files SHOULD either declare symbols (classes, functions, constants) OR cause side-effects (generate output, modify settings, etc.) but SHOULD NOT do both
2. Namespace and Class Names
- Classes MUST be declared in
StudlyCaps
- Class constants MUST be declared in all upper case with underscore separators
3. Class Methods
- Method names MUST be declared in
camelCase
Practical Implementation
Let’s look at a correct PSR-1 implementation from our example repository:
<?php
namespace JonesRussell\PhpFigGuide\PSR1;
class UserManager
{
const VERSION = '1.0.0';
const ERROR_TYPE_NOT_FOUND = 'not_found';
public function getUserById($id)
{
// Implementation
return ['id' => $id, 'name' => 'John Doe'];
}
}
This example demonstrates:
- Proper namespace declaration using
StudlyCaps
- Class name in
StudlyCaps
- Constants in uppercase with underscores
- Method name in
camelCase
Common Violations and Fixes
Mixed Responsibilities
Incorrect Naming
Integration with Modern PHP Tools
Our example repository includes setup for:
- PHP_CodeSniffer for PSR-1 validation (
composer check-style
) - Automated style fixing (
composer fix-style
) - PHPUnit for testing implementations
- Composer autoloading following PSR-4
Next Steps
In our next post, we’ll explore PSR-12, which extends these basic coding standards with more comprehensive style guidelines. This post is part of our PSR Standards in PHP series.
Resources
- Official PSR-1 Specification
- PHP_CodeSniffer PSR-1 Ruleset
- Series Example Repository (v0.1.0 - PSR-1 Implementation)
standards Article's
30 articles in total
PSR-6: Caching Interface in PHP
read article
PSR-4: Autoloading Standard in PHP
read article
PSR-3: Logger Interface in PHP
read article
PSR Standards in PHP: A Practical Guide for Developers
read article
PSR-1: Basic Coding Standard in PHP
currently reading
Anvil: An attempt of saving time
read article
Wednesday Links - Edition 2024-03-27
read article
The TAG, and Responsible Innovation on the Web
read article
2023 Industry Trends in Mobile Application User Interface
read article
Becoming W3C Games Community Group co-chair
read article
Jim's Guide to CockroachDB Naming Standards
read article
Writing Code with Standards and generate report on PreCommit : PHP / Laravel Project
read article
Creating a code style guide
read article
Call for Mentors for the Web Mapping Code Sprint: 29/11 - 01/12 2022 🎓
read article
Apeleg join the W3C
read article
2. Green Mode Design: Implementation Strategies
read article
Call for Mentors for the Vector Data Code Sprint: 12-14 July 2022 🎓
read article
Re-evaluating technology
read article
Web development is like assembling IKEA furniture
read article
Introducing the Email Markup Consortium (EMC)
read article
Pizza Code
read article
Estilo de código no PHP: as recomendações da PSR-1 e PSR-12
read article
Today, the distant future
read article
PHPArkitect: Put your architectural rules under test!
read article
Cross browser speech synthesis - the hard way and the easy way
read article
Foundations
read article
XML, making everything just a little bit harder.
read article
Coding Standards and Naming Conventions
read article
Portals and giant carousels
read article
Continuous partial browser support
read article
Featured ones: