PHP Code Style (PSR Standards)
In this page:
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
PSR-4 Autoloading
PSR-4 standardize करता है कि एक namespace एक folder structure पर कैसे map होता है, जो exact convention है जिस पर Composer का autoloader प्रति package किसी manual configuration के बिना classes ढूँढने और load करने के लिए depend करता है।
उदाहरण: PSR-4 Autoloading
<?php
// namespace App\Models; --> maps to src/Models/ per composer.json's psr-4 config
echo "Namespace structure mirrors folder structure";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
PSR-7 HTTP Messages
PSR-7 standardize करता है कि HTTP requests और responses objects की तरह कैसे represent होते हैं, जो अलग-अलग packages से middleware को standard follow करने वाले frameworks में साथ compose होने देता है।
उदाहरण: PSR-7 HTTP Messages
<?php
// interface RequestInterface { function getMethod(); function getUri(); }
echo "Standardizes HTTP requests/responses as objects across frameworks";
?>
Login to try C/C++/Java/PHP code in the editor
- tabs और spaces mix करना जब PSR-12 को चार spaces चाहिए।
- classes को ऐसे नाम देना जो file name से match न करे, जो PSR-4 autoloading तोड़ देता है।
- closing
?>rule भूल जाना, जिसे PSR-12 pure PHP files में omit करने को कहता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: