PHP PSR-1 Basic Code specification

Source: Internet
Author: User
Basic code Specification

This specification formulates the relevant standards for the basic elements of Code,
To ensure a high degree of technical interoperability between shared PHP code.

Keywords "must" ("must"), "must not/must not be" ("must not"), "Need" ("REQUIRED"),
"Will" ("Shall"), "no" ("Shall not"), "should" ("should"), "shouldn't" ("should not"),
A detailed description of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in [RFC 2119][].

Overview

The PHP code file must be

PHP code files must be UTF-8 encoded without a BOM;

The PHP code should only define classes, functions, constants, and other declarations, or other actions that would have a subordinate effect (such as generating file output and modifying. ini configuration files, etc.), which can only be selected;

Namespaces and classes must conform to the automatic loading specification for PSR: psr-0[];

The name of the class must follow the Hump naming specification at the beginning of studlycaps capitalization;

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

The method name must conform to the CamelCase-style lowercase start hump naming specification.

File

2.1. PHP tags

PHP code must use long tags or short output labels;
You must not use other custom tags.

2.2. Character encoding

The PHP code must be and can only be encoded with UTF-8 without a BOM.

2.3. Dependency effect (side effects)

A php file should either define only new declarations, such as classes, functions, or constants that do not have a subordinate effect, or only logical operations that produce a subordinate effect, but not both.

The term "subordination effect" (side effects) means that simply by including a file, you do not directly declare the class,
Functions, constants, and so on, while performing logical operations.

The "dependent effects" include, but are not limited to, generating output, direct require or include, connecting to external services, modifying INI configuration, throwing errors or exceptions, modifying global or static variables, reading or writing files, and so on.

Here is a counter example, a copy of the code that contains the declaration and produces the dependency effect:

    1. Dependency effect: Modifying INI configuration
    2. Ini_set (' error_reporting ', e_all);
    3. Dependency Effect: Introducing files
    4. Include "file.php";
    5. Dependency effect: Generate output
    6. echo "\ n";
    7. declaring functions
    8. function foo ()
    9. {
    10. function Body part
    11. }
Copy Code

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

    1. declaring functions
    2. function foo ()
    3. {
    4. function Body part
    5. }
    6. Conditional Declaration * * Not a subordinate effect
    7. if (! function_exists (' Bar ')) {
    8. function Bar ()
    9. {
    10. function Body part
    11. }
    12. }
Copy Code

Namespaces and classes

Namespaces and the naming of classes must follow [psr-0][].

By specification, each class is independent of one file, and the namespace has at least one hierarchy: the top-level organization name (vendor name).

The name of the class must follow the hump naming specification at the beginning of studlycaps capitalization.

PHP 5.3 and later versions of the code must use a formal namespace.

For example:

    1. PHP 5.3 and later versions of the notation
    2. namespace Vendor\model;
    3. Class Foo
    4. {
    5. }
Copy Code

The 5.2.x and previous versions should use the pseudo-namespace notation, which is conventionally used with the top-level organization name (vendor name), such as Vendor_, as the class prefix.

    1. 5.2.x and previous versions of the notation
    2. Class Vendor_model_foo
    3. {
    4. }
Copy Code

Constants, properties, and methods of a class

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

4.1. Constants

All letters in a class's constants must be capitalized, and the words are underlined.
Refer to the following code:

    1. namespace Vendor\model;
    2. Class Foo
    3. {
    4. Const VERSION = ' 1.0 ';
    5. Const date_approved = ' 2012-06-01 ';
    6. }
Copy Code4.2. Properties

A class's property name can follow a camel ($StudlyCaps) with uppercase, a camel ($camelCase) at the beginning of a lowercase, or an underscore ($under _score), and this specification does not require enforcement, but regardless of which naming method you follow, you should remain consistent within a certain range. This range can be the entire team, the entire package, the entire class, or the entire method.

4.3. Methods

The method name must conform to the CamelCase ()-style lowercase start hump naming specification.

Turn from GitHub (Pizzaliu)

PHP, PSR
  • 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.