What is PSR and Why Standards Matter
What is PSR?
PSR stands for PHP Standards Recommendations. They are specifications created by the PHP-FIG (Framework Interop Group) to standardize how PHP code is written.
Why Standards Matter
Without standards: With standards:
Team A uses tabs Everyone uses same style
Team B uses spaces Code looks identical across
Team C uses camelCase projects and teams
Team D uses snake_case Libraries work together
Libraries can't integrate Easy to switch projects
New developers struggle Quick onboarding
Key PSR Standards
| PSR | Name | Purpose |
|---|---|---|
| PSR-1 | Basic Coding Standard | File naming, class naming, method naming |
| PSR-4 | Autoloading | Map namespaces to directories |
| PSR-12 | Extended Coding Style | Indentation, spacing, visibility |
| PSR-7 | HTTP Message | Request/Response interfaces |
| PSR-3 | Logger Interface | Standard logging interface |
| PSR-11 | Container Interface | Dependency injection containers |
| PSR-14 | Event Dispatcher | Event handling |
| PSR-16 | Simple Cache | Cache interfaces |
Benefits of PSR Standards
1. INTEROPERABILITY
Libraries following PSR standards work together seamlessly
A PSR-4 autoloader loads any PSR-4 compliant package
2. CONSISTENCY
Code looks the same regardless of who wrote it
Easier to read and maintain
3. REUSABILITY
Code can be extracted into packages
Packages can be shared via Composer
4. COLLABORATION
Teams can work together without style debates
Focus on logic, not formatting
5. TOOLING
Static analyzers (PHPStan, Psalm) work better
Code formatters (PHP-CS-Fixer) can auto-fix style
PSR Compliance in Magento
<?php
// Magento follows PSR-1: One class per file
// File: Product.php contains only Product class
class Product
{
// Magento follows PSR-12: Proper formatting
private string $name; // Visibility declared
private float $price; // Type declared
public function __construct(
string $name,
float $price
) {
$this->name = $name;
$this->price = $price;
}
// Magento follows PSR-4: Namespace matches directory
// Vendor\Module\Model\Product -> Model/Product.php
}
Where to Learn More
Official PSR website: https://www.php-fig.org/psr/
PSR-1: https://www.php-fig.org/psr/psr-1/
PSR-4: https://www.php-fig.org/psr/psr-4/
PSR-12: https://www.php-fig.org/psr/psr-12/
PSR-7: https://www.php-fig.org/psr/psr-7/
Key Takeaway
PSR standards ensure PHP code is consistent, interoperable, and maintainable. Magento follows PSR-1, PSR-4, PSR-12, and other standards. Understanding these standards makes you a better PHP developer.
PSR-1, PSR-4, PSR-12, and PSR-7 Overview
PSR-1: Basic Coding Standard
<?php
// PSR-1 Rules:
// 1. Files must use <?php or <?= tags
// 2. Files must use UTF-8 without BOM
// 3. Files should either declare symbols or have side effects
// (not both)
// 4. Class names MUST be declared in StudlyCaps
nclass ProductRepository {} // Correct
nclass productRepository {} // Wrong
// 5. Class constants MUST be declared in upper case
nclass Status
{
const ACTIVE = 1; // Correct
const active = 1; // Wrong
}
// 6. Method names MUST be declared in camelCase
nclass Product
{
public function getPrice() {} // Correct
public function get_price() {} // Wrong
}
PSR-4: Autoloading
<?php
// PSR-4 Rules:
// 1. Namespace prefix maps to base directory
// 2. Subdirectories follow namespace segments
// 3. Class name matches filename (case-sensitive)
// 4. One class per file
// composer.json:
// "autoload": {
// "psr-4": {
// "Vendor\\Module\\": "src/"
// }
// }
// Vendor\Module\Product
// -> src/Product.php
// Vendor\Module\Helper\Data
// -> src/Helper/Data.php
// Vendor\Module\Model\ResourceModel\Product\Collection
// -> src/Model/ResourceModel/Product/Collection.php
PSR-12: Extended Coding Style
<?php
// PSR-12 Rules:
// 1. Code MUST use 4 spaces for indenting
// 2. There MUST NOT be a hard limit on line length
// 3. There MUST be one blank line after namespace declaration
// 4. There MUST be one use keyword per declaration
// 5. There MUST be one blank line after the use block
namespace Vendor\Module\Model;
use Magento\Framework\Model\AbstractModel; // One per line
use Magento\Catalog\Api\ProductRepositoryInterface;
use Psr\Log\LoggerInterface;
class Product extends AbstractModel
{
// 6. Visibility MUST be declared on all properties and methods
private string $name;
private float $price;
// 7. Method and function arguments with default values MUST go at end
public function __construct(
string $name,
float $price,
string $sku = '' // Default at end
) {
$this->name = $name;
$this->price = $price;
}
// 8. Method braces MUST go on the next line
// 9. Method body MUST be on the next line after opening brace
public function getFormattedPrice(): string
{
return '\$' . number_format($this->price, 2);
}
// 10. Control structure keywords MUST have one space after
if ($this->price > 0) {
return true;
}
// 11. Opening braces for control structures go on same line
// 12. Opening braces for classes and methods go on next line
}
PSR-7: HTTP Messages
<?php
// PSR-7 defines interfaces for HTTP requests and responses
// Used by Magento for API and middleware
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
// Request interface
$request->getMethod(); // GET, POST, etc.
$request->getUri(); // URI object
$request->getHeaders(); // Array of headers
$request->getBody(); // Stream body
// Response interface
$response->getStatusCode(); // 200, 404, etc.
$response->getHeaders(); // Array of headers
$response->getBody(); // Stream body
// URI interface
$uri->getScheme(); // https
$uri->getHost(); // magento-store.com
$uri->getPath(); // /catalog/product
$uri->getQuery(); // id=123
Key Takeaway
PSR-1: Basic naming conventions. PSR-4: Namespace-to-directory mapping. PSR-12: Detailed formatting rules. PSR-7: HTTP message interfaces. Magento follows all of these.
Quiz
1. What does PSR stand for in PHP?
2. Which PSR standard defines autoloading?
3. According to PSR-1, how should class names be declared?
4. What does PSR-12 specify about indentation?
5. What interfaces does PSR-7 define?
Flashcards
Question
What is PSR?
Click to reveal answer
Answer
PHP Standards Recommendations - specifications by PHP-FIG that standardize how PHP code is written. Key standards: PSR-1, PSR-4, PSR-12, PSR-7.
Question
What does PSR-1 specify?
Click to reveal answer
Answer
Basic coding standard: class names in StudlyCaps, method names in camelCase, constants in UPPER_CASE, one class per file.
Question
What does PSR-4 specify?
Click to reveal answer
Answer
Autoloading standard: maps namespace prefixes to directories. Vendor\Module\Product -> src/Product.php.
Question
What does PSR-12 specify?
Click to reveal answer
Answer
Extended coding style: 4 spaces indentation, one use per declaration, visibility on all methods/properties, braces on next line for classes/methods.
Question
What does PSR-7 define?
Click to reveal answer
Answer
HTTP message interfaces: RequestInterface, ResponseInterface, UriInterface. Used for PSR-15 middleware and HTTP clients.
Question
What is PHP-FIG?
Click to reveal answer
Answer
PHP Framework Interop Group - the organization that creates and maintains PSR standards. Members include Symfony, Laravel, Magento, and others.
Question
Why do PSR standards matter?
Click to reveal answer
Answer
They ensure code consistency, interoperability between libraries, easier collaboration, and better tooling support (static analysis, code formatting).
Question
How does Magento follow PSR standards?
Click to reveal answer
Answer
Magento follows PSR-1 (naming), PSR-4 (autoloading), PSR-12 (code style), and PSR-7 (HTTP messages). Its codebase is PSR-compliant.
Revision Notes
Key Takeaways
- 1. PSR = PHP Standards Recommendations by PHP-FIG
- 2. PSR-1: Basic naming (StudlyCaps classes, camelCase methods)
- 3. PSR-4: Namespace-to-directory mapping for autoloading
- 4. PSR-12: Extended coding style (4 spaces, visibility, braces)
- 5. PSR-7: HTTP message interfaces (Request, Response, Uri)
- 6. Standards ensure consistency, interoperability, and maintainability
- 7. Magento follows all major PSR standards
Interview Tips
- • Explain what PSR is and why it matters
- • Know the key differences between PSR-1, PSR-4, PSR-12, PSR-7
- • Describe how PSR-4 autoloading works
- • Give examples of PSR-12 coding style rules
- • Explain how Magento follows PSR standards
Cheat Sheet
PSR Standards Cheat Sheet
PSR-1 (Basic):
- Classes: StudlyCaps (ProductRepository)
- Methods: camelCase (getPrice)
- Constants: UPPER_CASE (ACTIVE)
- One class per file
PSR-4 (Autoloading):
- Namespace prefix -> directory
- Vendor\Module\Product -> src/Product.php
PSR-12 (Style):
- 4 spaces indentation
- One use per declaration
- Visibility on all methods/properties
- Braces on next line for classes/methods
PSR-7 (HTTP):
- RequestInterface
- ResponseInterface
- UriInterface
- StreamInterface