← Back to PHP Course | Chapter 16: Testing & Tools | Lesson 3 of 10

PHP Code Style (PSR Standards)

PSR standards PHP code के लिए shared writing rules हैं, एक group story में same spelling और layout इस्तेमाल करने पर agree करने जैसे। वे अलग-अलग लोगों के code को पढ़ना आसान बनाते हैं।

PSR Standards का Introduction

PSRs (PHP Standard Recommendations) PHP-FIG group से community-agreed conventions हैं जो code formatting से लेकर interfaces तक हर चीज़ cover करती हैं, independently लिखे गए packages को smoothly interoperate करने देते हुए।

उदाहरण: Introduction to PSR Standards

php
<?php
// Print "PSRs are community-agreed conventions from PHP-FIG for interoperability" to the output
echo "PSRs are community-agreed conventions from PHP-FIG for interoperability";
?>

PSR-12 Code Style

PSR-12 spacing, brace placement, और method signatures के लिए exact rules define करता है, ताकि अलग-अलग developers या packages का code consistently पढ़े बजाय सबके अपनी personal style follow करने के।

उदाहरण: PSR-12 Code Style

php
<?php
// Define the class `UserController`
class UserController
{
    // Define the function `show` taking `int $id`
    public function show(int $id): void
    {
        // Print `$id` to the output
        echo $id;
    }
}
// Print "Consistent spacing and brace placement, regardless of author" to the output
echo "Consistent spacing and brace placement, regardless of author";
?>

PSR-4 Autoloading

PSR-4 standardize करता है कि एक namespace एक folder structure पर कैसे map होता है, जो exact convention है जिस पर Composer का autoloader प्रति package किसी manual configuration के बिना classes ढूँढने और load करने के लिए depend करता है।

उदाहरण: PSR-4 Autoloading

php
<?php
// namespace App\Models; --> maps to src/Models/ per composer.json's psr-4 config
echo "Namespace structure mirrors folder structure";
?>

PSR-3 Logger Interface

PSR-3 एक standard logging interface define करता है (info(), warning(), error() जैसे methods के साथ), ताकि एक library 'कोई भी PSR-3 logger' accept कर सके और Monolog, एक custom logger, या इसे implement करने वाली किसी भी दूसरी चीज़ के साथ काम करे।

उदाहरण: PSR-3 Logger Interface

php
<?php
// Define the interface `LoggerInterface` describing required methods
interface LoggerInterface {
    // Define the function `info` taking `$message`
    function info($message);
    // Define the function `warning` taking `$message`
    function warning($message);
    // Define the function `error` taking `$message`
    function error($message);
}
// Define the class `SimpleLogger`, implementing `LoggerInterface`
class SimpleLogger implements LoggerInterface {
    // Define the function `info` taking `$message`
    function info($message) { echo "INFO: $message"; }
    // Define the function `warning` taking `$message`
    function warning($message) { echo "WARNING: $message"; }
    // Define the function `error` taking `$message`
    function error($message) { echo "ERROR: $message"; }
}
(new SimpleLogger())->info("App started");
?>

PSR-7 HTTP Messages

PSR-7 standardize करता है कि HTTP requests और responses objects की तरह कैसे represent होते हैं, जो अलग-अलग packages से middleware को standard follow करने वाले frameworks में साथ compose होने देता है।

उदाहरण: PSR-7 HTTP Messages

php
<?php
// interface RequestInterface { function getMethod(); function getUri(); }
echo "Standardizes HTTP requests/responses as objects across frameworks";
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. tabs और spaces mix करना जब PSR-12 को चार spaces चाहिए।
  2. classes को ऐसे नाम देना जो file name से match न करे, जो PSR-4 autoloading तोड़ देता है।
  3. closing ?> rule भूल जाना, जिसे PSR-12 pure PHP files में omit करने को कहता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.