PHP PSR-1 Basic Code Specification (Chinese version)

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"),
Detailed descriptions of "recommended" ("RECOMMENDED"), "Can" ("may") and "optional" ("OPTIONAL") can be found in RFC 2119.

1. Overview
    • PHP code files must <?php start with or <?= tag;

    • The PHP code file must be 不带BOM的 UTF-8 encoded;

    • PHP code should only define classes, functions, constants, and other declarations, or other 从属效应 operations (such as generating file output and modifying. ini configuration files), which can only be selected;

    • Namespaces and classes must conform to the automatic loading specification for PSR: PSR-0 or PSR-4;

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

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

    • The method name must conform camelCase to the lowercase start of the hump-naming specification.

2. Document 2.1. PHP tags

PHP code must use <?php ?> long tags or <?= ?> short output labels;
You must not use other custom tags.

2.2. Character encoding

The PHP code must be used and only 不带BOM的UTF-8 encoded.

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 effect" includes, but is not limited to, generating output, directly require or include connecting 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:

<?php// 从属效应:修改 ini 配置ini_set(‘error_reporting‘, E_ALL);// 从属效应:引入文件include "file.php";// 从属效应:生成输出echo "// 声明函数function foo(){ // 函数主体部分}

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

<?php// 声明函数function foo(){ // 函数主体部分}// 条件声明**不**属于从属效应if (! function_exists(‘bar‘)) { function bar() { // 函数主体部分 }}
3. 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 StudlyCaps the Hump naming specification at the beginning of the capitalization.

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

For example:

<?php// PHP 5.3及以后版本的写法namespace Vendor\Model;class Foo{}

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

<?php// 5.2.x及之前版本的写法class Vendor_Model_Foo{}
4. 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:

<?phpnamespace Vendor\Model;class Foo{ const VERSION = ‘1.0‘; const DATE_APPROVED = ‘2012-06-01‘;}
4.2. Properties

The property naming of a class can follow the camel-like () $StudlyCaps , lower-case-beginning camel () $camelCase or underscore-delimited ( $under_score ), but this specification does not impose a mandatory requirement, but it should be consistent within a certain scope regardless of which naming method is followed. 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 camelCase() to the lowercase start of the hump-naming specification.

因psr-0已废弃故没有转

Original: Pizzaliu GitHub

PHP PSR-1 Basic Code Specification (Chinese version)

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.