The development process of PSR Norm

Source: Internet
Author: User
Tags autoloader

====================PSR-0 (Automatic loading specification)=======================

The PSR-0 (autoloading standard) class auto-load specification, which is now obsolete (Deprecated), will be replaced by PSR-4.

1. A fully qualified namespace and class name must follow the following structure "\vendorname\namespace\classname"

2. Each namespace must have a top-level namespace of "VendorName"

3. Each namespace can have any number of sub-namespaces

4. Each namespace must be converted to the operating system path delimiter (Directory_separator) when it is loaded by the file system

5. Each "_" character is converted to Directory_separator in the class name. The use of underscores in PSR-4 does not have any special meaning

6. Namespaces and class names that conform to the naming criteria must be loaded with ". PHP" at the end of the file

7. Namespaces and class names can consist of uppercase and lowercase letters, but must be case sensitive to ensure multiple system compatibility

====================PSR-1 (Basic code specification)=======================

PSR-1 (Basic Coding Standard) Basic code specification to ensure a high degree of technical interoperability between shared PHP code.

1.PHP code source files must start with <?php or <?= tags

2.PHP code source file must use UTF-8 encoding without BOM

3. A source file is recommended only as a definition of classes, functions, constants and other declarations, or other operations that produce subordinate effects (such as: output information, modify configuration files, etc.)

4. Namespaces and classes must conform to the automatic load specification for PSR: PSR-0 or PSR-4

5. Class naming must follow the hump-type naming specification at the beginning of studlycaps capitalization

6. Constants in a class all letters must be capitalized, and the words are separated by underscores

7. Method names must conform to the CamelCase-style lowercase start hump-style naming specification

* The BOM (byte order mark) is part of the Unicode standard and is typically used to mark the plain text byte order (byte order), allowing the text handler to recognize the Unicode encoding used by the read-in file (UTF-8, UTF-16, UTF-32).

* Dependent effects are logical operations that are performed only by including files, not directly declaring classes, functions, and constants. A PHP source file should either contain only defined operations that do not have a dependency effect, or contain logical operations that only produce a subordinate effect, and do not include both.

====================PSR-2 (Code style specification)=======================

PSR-2 (Coding style guide) code style specification, through the development of a series of rules to standardize the PHP code, in order to reduce the author code style differences caused by the reading inconvenience.

1. The code must follow the coding specifications in PSR-1

2. The code must be indented using 4 spaces, not tabs (tab)

3. It is recommended that each line of code characters remain within 80, theoretically not more than 120, but do not make a hard limit

4. A blank line must be inserted after each namespace namespace statement and use declaration statement block

5. The left curly brace "{" Of the class must be written in a row after the declaration, and the right curly brace "}" must also be self-formed under the class body

6. The left curly brace "{" of the method must be placed in a row after the declaration, and the right curly brace "}" must also be self-contained under the body

7. The properties and methods of the class must add an access modifier (private, protected, public), abstract, and final must be declared before the access modifier, and static must be declared after the access modifier (for example: final public Static

8. There must be a space behind the control structure keyword, and the method or function cannot be called (Control structure: If-else, Switch-case, Try-catch, while, foreach ...). )

9. The left curly brace "{" of the control structure must be in the same line, and the closing curly brace "}" must be self-contained after controlling the structure body

10. Control structure after the opening parenthesis, and the end of the closing parenthesis can not have spaces

====================PSR-3 (Log interface Specification)=======================

PSR-3 (Logger Interface) Log Interface specification, the main purpose is to let the log class library by receiving a Loggerinterface object to log information.

The 1.LoggerInterface interface defines eight methods for recording the eight-level logs defined in RFC 5424, respectively: Debug, info, notice, warning, error, critical, alert, emergency

2. The Ninth method log (), the first parameter is the record level. This method can be called using a predefined rank constant as a parameter and must have the same effect as calling the eight methods directly. If an incoming rank constant is not predefined, you must throw an exception of type 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 (automatic loading new rules)=======================

PSR-4 (improved autoloading) This specification is about automatically loading corresponding classes of the relevant specifications, is the PSR-0 automatic loading specification supplement.

1. The "class" Here is a general term that contains classes, interfaces, traits, and other similar structures

2. The fully qualified class name needs to follow the following structure:\<命名空间>(\<子命名空间>)*\<类名>

* Fully qualified class name must have a top-level namespace, known as "Vendor namespace";

* Fully qualified class names can have one or more sub-namespaces;

* The fully qualified class name must have a terminating class name;

* The underscore in any part of a fully qualified class name has no special meaning;

* The fully qualified class name can consist of any uppercase and lowercase letters;

* Fully qualified class names must be referred to in case-sensitive manner;

3. When loading the corresponding file according to the full class name:

* A namespace prefix (excluding the delimiter of the top-level namespace) that consists of contiguous one or several sub-namespaces in a fully qualified class name, at least one underlying directory;

* The child namespace immediately following the namespace prefix must match the corresponding "file base directory", where the namespace delimiter will be used as the directory delimiter;

* The terminating class name corresponds to a file ending in. php, and the filename must match the terminating class name case;

4. The implementation of the autoloader (autoloader) cannot throw an exception, cannot raise any level of error, and should not have a return value

The development process of PSR Norm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.