On this page
PHP Standard Recommendations (PSRs)
PHP Standard Recommendations (PSRs) are guidelines published by the PHP-FIG (PHP Framework Interoperability Group) to standardize PHP code and improve interoperability. Below is a summary of the most commonly referenced PSRs:
PSR-1: Basic Coding Standard
- Purpose: Defines basic coding standards for PHP.
- Key Points:
- Files must use only
<?php
and<?=
tags. - Class names should be in StudlyCaps.
- Method names should be in camelCase.
- Constants should be in uppercase with underscores separating words.
- Files must use only
PSR-2: Coding Style Guide
- Purpose: Provides guidelines for coding style to improve readability and consistency.
- Key Points:
- Code should be indented with 4 spaces.
- Lines should not exceed 120 characters.
- Braces should be on the same line as the declaration.
- Method and function names should be in camelCase.
PSR-3: Logger Interface
- Purpose: Defines a standard interface for logging libraries.
- Key Points:
- Provides
Psr\Log\LoggerInterface
with methods likeemergency()
,alert()
,critical()
,error()
, etc. - Ensures consistency across different logging libraries.
- Provides
PSR-4: Autoloading Standard
- Purpose: Specifies a standard for autoloading classes using namespaces.
- Key Points:
- Classes should be in files that match their fully qualified class name.
- Allows for autoloading based on namespace and class name.
PSR-7: HTTP Message Interface
- Purpose: Defines interfaces for HTTP messages, including requests and responses.
- Key Points:
- Defines
Psr\Http\Message\RequestInterface
andPsr\Http\Message\ResponseInterface
. - Provides methods for working with HTTP headers, body, and status codes.
- Defines
PSR-12: Extended Coding Style Guide
- Purpose: Extends PSR-2 to provide a more comprehensive coding style guide.
- Key Points:
- Builds upon PSR-2 with additional rules and guidelines.
- Enhances code consistency and readability.
PSR-15: HTTP Server Request Handlers
- Purpose: Defines interfaces for handling HTTP server requests.
- Key Points:
- Provides
Psr\Http\Server\RequestHandlerInterface
for handling requests and generating responses.
- Provides
PSR-16: Simple Cache
- Purpose: Defines a simple caching interface.
- Key Points:
- Provides
Psr\SimpleCache\CacheInterface
with methods for basic cache operations likeget()
,set()
,delete()
, etc.
- Provides
PSR-17: HTTP Factories
- Purpose: Provides factories for creating PSR-7 compliant HTTP messages.
- Key Points:
- Includes interfaces for creating request, response, and server request objects.
PSR-18: HTTP Client
- Purpose: Defines a standard interface for HTTP clients.
- Key Points:
- Provides
Psr\Http\Client\ClientInterface
for sending HTTP requests and receiving responses.
- Provides