Transferred from: http://www.cnblogs.com/x3d/p/php-psr-standards.html
Psr
PHP Standard recommendation shorthand, it should be called PSRs, that is, the series of recommended standards: the current adoption of the specification has PSR-0 (autoloading), PSR-1 (Basic Coding) , PSR-2 (Coding Style Guide), PSR-3 (Logger Interface), PSR-4 (improved autoloading). It is not the official PHP standard, but a series of standards extracted from well-known PHP projects such as Zend, Symfony2, and now more and more community projects are joining members and following that standard.
php-fig-php Framework Interoperability Group is an organization that formulates PSRS development specifications, and its members are made up of many well-known PHP community projects, and a list of members can be found at the bottom of their homepage. These members develop specifications and are implemented in their own projects.
In fact, the content of each PSR specification is very concise and clear, compared to those who move the dozens of-page specification, is really good too much.
PSR-0 (autoloading Standard)
PSR-0-Class Automatic Loading specification (original: official website, GitHub). From 2014-10-21 onwards, the specification was marked as Deprecatedand replaced by PSR-4. Its contents are very concise.
Content:
- A fully qualified namespace and class name must have the following structure "\<
Vendor Name
>\ (< Namespace
>\) *< Class Name
>"
- Each namespace must have a top-level namespace ("Vendor name")
- Each namespace can have any number of sub-namespaces
- Each namespace must be converted to the operating system path delimiter (Directory_separator) when it is loaded from the file system
- Each "_" character is converted to Directory_separator in the class name. The "_" symbol has no explicit meaning in the namespace
- Namespaces and class names that conform to the naming standard must be loaded with the end of ". PHP"
Vendor Name
, a namespace in which the class name can consist of uppercase and lowercase letters, where the namespace and class names are case sensitive to ensure multiple system compatibility
PSR-1 (Basic Coding Standard)
PSR-1 is the basic coding standard (original: website, GitHub). Inside Rong is also very concise, including the class file, class name, class method name naming methods.
Content:
The source file must use only the <?php and <?= labels
The encoding format of the PHP code in the source file must only be UTF-8 with no BOM
A source file is recommended only for declarations (classes, functions, constants, etc.) or only for actions that cause side effects (for example, output information, modify. ini configuration files, etc.), but should not do both things at the same time
Namespaces and classes must comply with the PSR-0 standard
Class names must be written using StudlyCaps
Constants in a class must consist only of uppercase letters and underscores (_)
Method names must be written using the camecase notation
PSR-2 (Coding Style Guide)
PSR-2 is the coding style standard (original: official website, GitHub). Slightly more content, but still relatively concise, it is based on PSR-1, including indentation, each line of code length, line wrapping, method visibility declarations, spaces, and method body braces for wrapping the relevant provisions.
Standard content:
Code must follow PSR-1
The code must use 4 spaces to indent instead of a tab
The length of a line of code should not have a hard limit, the soft limit must be 120 characters, it is recommended that each line of code 80 characters or less
There must be a blank line under the Declaration of the namespace, and there must be a line below the declaration of the Use
The left curly brace of a class must be placed in a row below its declaration, and the right curly brace must be placed in a row below the body of the class
The left curly brace of a method must be placed in a row below its declaration, and the right curly brace must be placed on the next line of the method body
All properties and methods must have a visibility declaration; the abstract and final declarations must precede the visibility declaration, and the static declaration must be preceded by a declaration of visibility
There must be a space after the structure control keyword, and no spaces after methods and function calls
The left curly brace of the structure control must be placed on the same line as the right curly brace must be placed on the next line of the structure control code body
No spaces after the left parenthesis of the control structure, no spaces before the right parenthesis
PSR-3 (Logger Interface)
PSR-3 is the definition of the pass-through interface to the Application log Class (original: Official website, GitHub). The content is simple, is an interface, the official sample code to reference it. Of course, in a specific application, as long as you follow this interface, you can certainly customize the corresponding implementation.
Basic content:
Loggerinterface exposes eight interfaces for logging eight levels (debug, info, notice, warning, error, critical, alert, emergency).
The Nineth method is log, which accepts the log level as the first parameter. Calling this method with a log rank constant must be consistent with the result of invoking the specified hierarchy method directly. Calling the method with a log level that is not defined in this specification and is not known for a specific implementation must throw a psr\log\invalidargumentexception. It is not recommended to use a custom log level unless you are very sure that the current class library has support for it.
PSR-4 (improved autoloading)
PSR-4 is an improved version of the automatic Loading specification (original: official website, GitHub). It is the successor to the PSR-0 specification. It can be compatible with any other auto-loading specification, including PSR-0.
Content:
- The term "class" is a general term; it contains classes, interfaces, traits, and other similar structures;
- The fully qualified class name should resemble the following example:<namespacename> (<SubNamespaceNames>) *<classname>
Full compliance class name must have a top-level namespace (Vendor name)
Full compliance class name can have many child namespaces
The full compliance class name should have a terminating class name
Underscores have no special meaning in the full compliance class name
Letters can be any combination of uppercase and lowercase in the full compliance class name
All class names must be case-sensitive to reference
- When loading a file from a fully compliant class name:
In a fully compliant class name, a contiguous namespace prefix (excluding the delimiter of the top-level namespace) consisting of one or more sub-namespaces that corresponds at least one base directory
The contiguous sub-namespace name after the namespace prefix corresponds to a subdirectory under the base directory, where the namespace delimiter represents the directory delimiter. The subdirectory name must match the child namespace name case
The terminating class name corresponds to a file that ends in. php. The file name must match the terminating class name case
The implementation of the automatic loader cannot throw any exceptions, cannot raise any level of error, and should not return a value
PHP Development Specification-PSR Series specifications