Home > Backend Development > PHP Tutorial > PHP PSR-1 basic code specification (Chinese version), _PHP tutorial

PHP PSR-1 basic code specification (Chinese version), _PHP tutorial

WBOY
Release: 2016-07-12 09:00:27
Original
889 people have browsed it

PHP PSR-1 Basic Code Specification (Chinese version),

Basic Code Specification

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

Keywords "MUST", "MUST NOT", "REQUIRED",
"will" ("SHALL"), "won't" ("SHALL NOT"), "should" ("SHOULD"), "should not" ("SHOULD NOT"),
Detailed descriptions of "RECOMMENDED", "MAY" and "OPTIONAL" can be found in RFC 2119.

1. Overview

  • PHP code file must start with <?php or <?= tag;

  • PHP code files must be encoded in 不带BOM的 UTF-8;

  • The in PHP code should only define declarations such as classes, functions, constants, etc., or other operations that will generate 从属效应 (such as generating file output and modifying .ini configuration files, etc.), both of which only You can choose one;

  • Namespaces and classes must conform to the PSR autoloading specification: one of PSR-0 or PSR-4;

  • The naming of the class must follow the camel case naming convention starting with StudlyCaps capital;

  • All letters of the constants in the class must be capitalized, and words should be separated by underscores;

  • The method name must conform to the naming convention of camelCase starting with lowercase camel case.

2. File

2.1. PHP tags

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

2.2. Character encoding

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

2.3. Subordinate effects (side effects)

A PHP file should either only define new declarations, such as classes, functions or constants, which do not produce dependency effects, or only logical operations that produce dependency effects, But shouldn’t have both at the same time.

The term "side effects" means that only by including the file without directly declaring the class,
Functions and constants, etc., while performing logical operations.

"Subordinate 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, etc. .

The following is a counter-example, a code containing a declaration and a dependent effect:

<code class="php"><span class="hljs-preprocessor"><?php
<span class="hljs-comment">// 从属效应:修改 ini 配置
ini_set(<span class="hljs-string">'error_reporting', E_ALL);

<span class="hljs-comment">// 从属效应:引入文件
<span class="hljs-keyword">include <span class="hljs-string">"file.php";

<span class="hljs-comment">// 从属效应:生成输出
<span class="hljs-keyword">echo <span class="hljs-string">"<html>\n";

<span class="hljs-comment">// 声明函数
<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">foo<span class="hljs-params">()
{
    <span class="hljs-comment">// 函数主体部分
}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

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

<code class="php"><span class="hljs-preprocessor"><?php
<span class="hljs-comment">// 声明函数
<span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">foo<span class="hljs-params">()
{
    <span class="hljs-comment">// 函数主体部分
}

<span class="hljs-comment">// 条件声明**不**属于从属效应
<span class="hljs-keyword">if (! function_exists(<span class="hljs-string">'bar')) {
    <span class="hljs-function"><span class="hljs-keyword">function <span class="hljs-title">bar<span class="hljs-params">()
    {
        <span class="hljs-comment">// 函数主体部分
    }
}
</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

3. Namespaces and classes

Namespace and class naming must follow PSR-0.

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

The naming of classes must follow the camel case naming convention of StudlyCaps starting with an uppercase letter.

Code for PHP 5.3 and later versions must use the official namespace.

For example:

<code class="php"><span class="hljs-preprocessor"><?php
<span class="hljs-comment">// PHP 5.3及以后版本的写法
<span class="hljs-keyword">namespace <span class="hljs-title">Vendor\<span class="hljs-title">Model;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Foo
{
}
</span></span></span></span></span></span></span></span></code>
Copy after login

Versions 5.2.x and earlier should use pseudo-namespace writing. It is a convention to use the top-level organization name (vendor name) such as Vendor_ as the class prefix.

<code class="php"><span class="hljs-preprocessor"><?php
<span class="hljs-comment">// 5.2.x及之前版本的写法
<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Vendor_Model_Foo
{
}
</span></span></span></span></span></code>
Copy after login

4. Class constants, properties and methods

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

4.1. Constants

All letters

in the constants of the class must be capitalized, and words are separated by underscores.
Refer to the following code:

<code class="php"><span class="hljs-preprocessor"><?php
<span class="hljs-keyword">namespace <span class="hljs-title">Vendor\<span class="hljs-title">Model;

<span class="hljs-class"><span class="hljs-keyword">class <span class="hljs-title">Foo
{
    <span class="hljs-keyword">const VERSION = <span class="hljs-string">'1.0';
    <span class="hljs-keyword">const DATE_APPROVED = <span class="hljs-string">'2012-06-01';
}
</span></span></span></span></span></span></span></span></span></span></span></code>
Copy after login

4.2. Properties

The attribute naming of the

class can follow the camel case format starting with an uppercase ($StudlyCaps), the camel case format starting with a lowercase case ($camelCase), or the underscore-delimited format ($under_score). This specification does not make it mandatory, but No matter which naming scheme is followed, it should be consistent within a certain scope. This scope can be the entire team, the entire package, the entire class, or the entire method.

4.3. Method

The method name

must conform to the naming convention of starting with lowercase camel case. camelCase()


因psr-0已废弃故没有转

Original text: PizzaLiu github

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1093108.htmlTechArticlePHP PSR-1 Basic Code Specification (Chinese version), Basic Code Specification This specification formulates the basic elements of the code Relevant standards to ensure a high degree of technical interoperability between shared PHP codes...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template