Junior — Middle
Can you explain what the PSR standard in PHP is and why it is needed?
sobes.tech AI
Answer from AI
PSR (PHP Standards Recommendations) is a set of standards and guidelines for writing PHP code, developed by PHP-FIG (PHP Framework Interop Group). The goal of PSR is to ensure uniformity and compatibility of code across different projects and frameworks.
Main reasons for using PSR:
- Improving code readability and maintainability.
- Facilitating integration and collaboration between different libraries.
- Simplifying standardization of development processes.
Examples of popular PSR standards:
- PSR-1: Basic coding standards (e.g., naming conventions).
- PSR-2: Coding style (indents, spaces, line breaks).
- PSR-4: Autoloading classes using namespaces.
Example of PSR-4 autoloading:
// composer.json
{
"autoload": {
"psr-4": {
"App\\": "src/"
}
}
}
This allows automatic loading of classes from the src folder corresponding to the App\ namespace, without manually including files.