PHPPSR-1 basic code specification (Chinese version), _ PHP Tutorial

Source: Internet
Author: User
PHPPSR-1 basic code specification (Chinese version ),. PHPPSR-1 basic code specification (Chinese version), basic code specification this specification has developed the relevant standards of the basic elements of the code, to ensure the sharing of PHP code with a high degree of technical intercommunication PHP PSR-1 basic code specifications (Chinese version ),
Basic code specification

This article provides standards for the basic elements of the code,
To ensure that the shared PHP code has a high degree of technical interconnectivity.

Key words: "MUST" ("MUST"), "must not/must not" ("must not"), "need" ("REQUIRED "),
"Will" ("SHALL"), "will NOT" ("shall not"), "SHOULD" ("shocould"), "shouldn't" ("shocould NOT "),
For detailed descriptions of "recommendations" ("RECOMMENDED"), "Yes" ("MAY"), and "OPTIONAL" ("OPTIONAL"), see RFC 2119.

1. Overview
  • PHP code fileRequiredTo Or Label start;

  • PHP code fileRequiredToUTF-8 without BOMEncoding;

  • PHP codeShouldOnly declarations such as classes, functions, and constants are defined.Subordination(For example, generating file output and modifying the. ini configuration file), either of them can be selected;

  • Namespaces and classesRequiredCompliant with automatic load specifications of the HDR: one in the PSR-0 or PSR-4;

  • Class nameRequiredFollowStudlyCapsUpper-case hump naming rules;

  • All the letters of constants in the class areRequiredUppercase: words are separated by underscores;

  • Method nameRequiredYescamelCaseType.

2. file 2.1. PHP tag

PHP codeRequiredUse Long tag or Short output tag;
Must notUse other custom tags.

2.2. Character encoding

PHP codeRequiredAnd can only be usedUTF-8 without BOMEncoding.

2.3. subordination (side effects)

A php fileShouldOtherwise, only new declarations, such as classes, functions, or constants, are defined, and only logical operations that produce the subordination effect are allowed.Shouldn'tBoth.

The word "side effects" means that classes,
Functions and constants.

The "subordinate effect" includes but is not limited to: generating output and directrequireOrinclude, Connect to external services, modify ini configurations, throw errors or exceptions, modify global or static variables, read or write files, etc.

The following is a counterexample, containing the declaration and code that produces the subordinate effect:


    \ N "; // declare function foo () {// function Subject}

The following is an example of a code that only contains declarations that do not produce a subordination effect:


    
3. namespaces and classes

The namespace and the class name must follow the PSR-0.

According to the specification, each class is independent of a file, and the namespace must have at least one level: top-level organization name (vendor name ).

The class name must followStudlyCapsDescription: description of the upper case.

Code for PHP 5.3 and later versionsRequiredUse the formal namespace.

For example:


     

5.2.x and earlier versionsShouldThe use of pseudo namespace is a convention that uses top-level organization names (vendor names) suchVendor_Is the class prefix.

 
4. constants, attributes, and methods of classes

The "class" here refers to all classes, interfaces, and reusable code blocks (traits)

4.1. constant

All the letters in the constant of the class areRequiredIn upper case, the words are separated by dashes.
Refer to the following code:


       
4.2. attributes

Class attribute names can follow the upper-case hump ($StudlyCaps($camelCase) Or an underline separator ($under_score), This specification is not mandatory, but no matter which naming method is followedShouldIn a certain range. This scope can be the entire team, the entire package, the entire class, or the entire method.

4.3. method

Method nameRequiredYescamelCase()Type.

Because the psr-0 has been abandoned, it is not transferred

Original article: PizzaLiu github

Http://www.bkjia.com/PHPjc/1093108.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1093108.htmlTechArticlePHP PSR-1 basic code specification (Chinese version), the basic code specification this specification has developed the relevant standards of the basic elements of the code, to ensure that the sharing of PHP code has a high degree of technical intercommunication...

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.