Logo

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
Categories
3 categories in total
php
open
standards
open
phpfig
open
Author
12 person written this
jonesrussell
open
PSR-1: Basic Coding Standard in PHP

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'];
    }
}

Enter fullscreen mode Exit fullscreen mode

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

  1. Mixed Responsibilities

  2. 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

standards Article's
30 articles in total
Favicon
PSR-6: Caching Interface in PHP
Favicon
PSR-4: Autoloading Standard in PHP
Favicon
PSR-3: Logger Interface in PHP
Favicon
PSR Standards in PHP: A Practical Guide for Developers
Favicon
PSR-1: Basic Coding Standard in PHP
Favicon
Anvil: An attempt of saving time
Favicon
Wednesday Links - Edition 2024-03-27
Favicon
The TAG, and Responsible Innovation on the Web
Favicon
2023 Industry Trends in Mobile Application User Interface
Favicon
Becoming W3C Games Community Group co-chair
Favicon
Jim's Guide to CockroachDB Naming Standards
Favicon
Writing Code with Standards and generate report on PreCommit : PHP / Laravel Project
Favicon
Creating a code style guide
Favicon
Call for Mentors for the Web Mapping Code Sprint: 29/11 - 01/12 2022 🎓
Favicon
Apeleg join the W3C
Favicon
2. Green Mode Design: Implementation Strategies
Favicon
Call for Mentors for the Vector Data Code Sprint: 12-14 July 2022 🎓
Favicon
Re-evaluating technology
Favicon
Web development is like assembling IKEA furniture
Favicon
Introducing the Email Markup Consortium (EMC)
Favicon
Pizza Code
Favicon
Estilo de código no PHP: as recomendações da PSR-1 e PSR-12
Favicon
Today, the distant future
Favicon
PHPArkitect: Put your architectural rules under test!
Favicon
Cross browser speech synthesis - the hard way and the easy way
Favicon
Foundations
Favicon
XML, making everything just a little bit harder.
Favicon
Coding Standards and Naming Conventions
Favicon
Portals and giant carousels
Favicon
Continuous partial browser support

Featured ones: