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

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

WBOY
WBOYOriginal
2016-07-12 09:00:27850browse

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 8926a7104e3211f45d47cf297485cf58 long tag or e001818591141260ad444db4de1b1edb 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>

    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>

    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>

    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>

    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>

    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...
Statement:
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