PSR-1 basic code specification

Source: Internet
Author: User
PSR-1 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.

1. Overview -----------

  • PHP code fileRequiredTo

  • PHP code fileRequiredIt is encoded with a UTF-8 without BOM;

  • PHP codeShouldOnly declarations such as classes, functions, and constants are defined, or other operations that will produce a subordinate effect (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 nameRequiredFollow the hump naming rules starting with StudlyCaps capital;

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

  • Method nameRequiredComplies with camelCase-based lower-case start hump naming rules.

  1. File
2.1. PHP tag

PHP codeRequiredUse Long tag or Short output tag;

You cannot use other custom tags.

2.2. Character encoding

PHP codeRequiredAnd only UTF-8 code without BOM can be used.

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 logical operations performed only by including files, without directly declaring classes, functions, and constants.

The "subordinate effect" is not limited: generate output, direct require or include, connect to external services, modify ini configuration, 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:

     
 
 
  1. 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 ).

Class names must follow the hump naming rules starting with StudlyCaps in 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 generally the use of top-level organization names (vendor name), such as the class prefix of Vendor.

       
   
   
  1. Class constants, attributes, and methods

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 camelCase ($ StudlyCaps), lower-case camelCase ($ camelCase), or underline ($ 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 nameRequiredComplies with camelCase ()-based lower-case start hump naming rules.

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.