PHPPSR-1 basic code specification

Source: Internet
Author: User
PHPPSR-1 basic code specification
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] [].

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 the automatic loading specification of the PSR: one of the PSR-0;

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.

File

2.1. PHP tag

PHP codeRequiredUse Long tag or Short output tag;
Must notUse 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 that 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:

  1. // Subordinate effect: modify the ini configuration
  2. Ini_set ('error _ report', E_ALL );
  3. // Subordinate effect: Introduce the file
  4. Include "file. php ";
  5. // Subordinate effect: generate output
  6. Echo"\ N ";
  7. // Declare a function
  8. Function foo ()
  9. {
  10. // Main part of the function
  11. }

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

  1. // Declare a function
  2. Function foo ()
  3. {
  4. // Main part of the function
  5. }
  6. // The condition declaration ** no ** is a subordinate effect.
  7. If (! Function_exists ('bar ')){
  8. Function bar ()
  9. {
  10. // Main part of the function
  11. }
  12. }

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:

  1. // PHP 5.3 and later versions
  2. Namespace Vendor \ Model;
  3. Class Foo
  4. {
  5. }

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. // 5.2.x and earlier versions
  2. Class Vendor_Model_Foo
  3. {
  4. }

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:

  1. Namespace Vendor \ Model;
  2. Class Foo
  3. {
  4. Const VERSION = '1. 0 ';
  5. Const DATE_APPROVED = '2017-06-01 ';
  6. }

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.