PHP-FIG's PSR Standards with Examples

The PHP Framework Interop Group (PHP-FIG) plays a key role in improving the interoperability of PHP components and libraries across different frameworks. One of the most important contributions of PHP-FIG is the PHP Standard Recommendations. PSR These standards provide guidance on coding style, autoloading, and more, enabling a consistent and collaborative approach to PHP development. In this article, we'll explore the leading PSR standards with examples and illustrate them with experience from my own projects. Let's get started!

1. PSR-1: Basic Coding Standard

PSR-1 specifies the fundamental rules PHP developers should follow to create a consistent and readable codebase. Key recommendations include:

  • For inline codes only ) jump.
  • Define namespaces and classes using appropriate capitalization.

Example:

<?php

namespace BenimAdAlanim;

class BenimSinif {
    // Sınıf uygulaması
}

These simple rules increased the readability of the code when I worked on a project and allowed my teammates to quickly understand my code.

2. PSR-2: Coding Style Guide

Building on PSR-1, PSR-2 offers more detailed coding style rules, ensuring a consistent look and feel across projects. It covers indentation, naming conventions, and more.

Example:

<?php

namespace BenimAdAlanim;

class BenimSinif
{
    const BENIM_SABIT = 'deger';

    protected $benimOzellik;

    public function benimMetod($parametre)
    {
        if ($kosul) {
            // Kod bloğu
        }
    }
}

When I wrote code using PSR-2 in a web application, I noticed how indentation and consistent naming kept the project organized. Having everyone in a team using the same style made the code easier to understand.

3. PSR-4: Automatic Loading Standard

Autoloading classes in PHP projects simplifies the inclusion of necessary files. PSR-4 defines autoloading rules based on namespaces and directory structure.

Example (Project structure: project/src/MyNamespace/MyClass.php):

PHP-FIG's PSR Standards: A Guide with Examples

Hello! The PHP Framework Interop Group (PHP-FIG) plays an important role in improving the interoperability of PHP components and libraries across different frameworks. One of the most important contributions of PHP-FIG is the PHP Standard Recommendations. PSR These standards provide guidance on coding style, autoloading, and more, enabling a consistent and collaborative approach to PHP development. In this article, we'll explore the leading PSR standards with examples and illustrate them with experience from my own projects. Let's get started!

1. PSR-1: Basic Coding Standard

PSR-1 specifies the fundamental rules PHP developers should follow to create a consistent and readable codebase. Key recommendations include:

  • For inline codes only <?php tags and use the closing tag (?>) jump.
  • Define namespaces and classes using appropriate capitalization.

Example:

PHP
<?php

namespace MyNamespace;

class MyClass {
    // Class implementation
}

These simple rules increased the readability of the code when I worked on a project and allowed my teammates to quickly understand my code.

2. PSR-2: Coding Style Guide

Building on PSR-1, PSR-2 offers more detailed coding style rules, ensuring a consistent look and feel across projects. It covers indentation, naming conventions, and more.

Example:

PHP
<?php

namespace MyNamespace;

class MyClass
{
    const MY_CONST = 'value';

    protected $myFeature;

    public function myMethod($parameter)
    {
        if ($condition) {
            // Code block
        }
    }
}

When I wrote code using PSR-2 in a web application, I noticed how indentation and consistent naming kept the project organized. Having everyone in a team using the same style made the code easier to understand.

3. PSR-4: Automatic Loading Standard

Autoloading classes in PHP projects simplifies the inclusion of necessary files. PSR-4 defines autoloading rules based on namespaces and directory structure.

Example (Project structure: project/src/MyNamespace/MyClass.php):

PHP
<?php

// Otomatik yükleyici kurulumu
spl_autoload_register(function ($sinifAdi) {
    $onEk = 'BenimAdAlanim\\';
    $temelDizin = __DIR__ . '/src/';

    $uzunluk = strlen($onEk);
    if (strncmp($onEk, $sinifAdi, $uzunluk) !== 0) {
        return;
    }

    $goreceliSinif = substr($sinifAdi, $uzunluk);
    $dosya = $temelDizin . str_replace('\\', '/', $goreceliSinif) . '.php';

    if (file_exists($dosya)) {
        require $dosya;
    }
});

// Kullanım
$benimNesne = new BenimAdAlanim\BenimSinif();

I used autoloading with PSR-4 when developing a library, and it made file management incredibly easy. My codebase became organized and modular.

4. PSR-7: HTTP Message Interface

This standard defines interfaces for HTTP messages such as requests and responses. It provides better interoperability between web components and frameworks.

Example:

<?php

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

function istegiIsle(ServerRequestInterface $istek): ResponseInterface {
    // İsteği işle ve bir yanıt oluştur
    // ...

    return $yanit;
}

I used PSR-7 to manage HTTP requests in an API project and saw how easy it was to integrate with different frameworks.

5. PSR-11: Container Interface

Containers are a common method for managing dependencies in PHP projects. PSR-11 defines an interface for accessing and managing dependencies in a consistent manner.

Example:

dependency = $container->get('DependencyClass'); } }

When I created a modular structure using PSR-11 in a dependency injection project, I found the code much easier to maintain and test.

Conclusion

PHP-FIG's PSR standards improve collaboration, maintainability, and code quality in PHP projects. From PSR-1 to PSR-11, these standards ensure consistency across libraries and frameworks. When I implemented these standards while developing a web application, I saw how organized and collaborative my codebase became. By adhering to these standards, you can contribute to a more robust and harmonious PHP ecosystem.

In which projects have you used PSR standards? Did you have an interesting experience? Share it in the comments so we can discuss it together! Check out my blog or contact me for more tips!

5 1 vote
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments