麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > PHP > 正文

PHP編碼風格指南(PHPFIGPSR2)

2020-03-22 19:41:44
字體:
來源:轉載
供稿:網友
  • 本指南是 PSR-1 基本編碼標準 的擴展。

    本指羅列了通用的PHP代碼格式規則和建議,意在減少不同作者的編碼風格差異帶來的認知障礙。

    這里的風格約定衍生自若干成員項目。指南作者們在多個項目中協作,推動了這些指導條款落地。 指南的關鍵在于共享,而不是規則本身。

    文中涉及的關鍵詞 “MUST 必須”, “MUST NOT 必須不”, “REQUIRED 必需”, “SHALL 會”, “SHALL NOT 不會”, “SHOULD 應該”, “SHOULD NOT 不應該”, “RECOMMENDED 推薦的”, “MAY 可能”, 和 “OPTIONAL 可選的” 在RFC 2119 中有具體描述.

    概覽代碼必需遵循 “基礎編碼標準” PSR [PSR-1]。代碼縮進必須使用 4 空格,而不是tab。行長度必須無硬性限制; 軟性限制必須為120字符;應該少于等于80字。namespace聲明后必須有一個空行,use聲明后也必須有一個空行。類的{ 必須在類名的下一行, }必須在body的下一行。方法的 { 必須在方法簽名的下一行,} 必須在body的下一行。所有的屬性和方法都要設置可見性; abstract和 final必須在可見性之前聲明;html' target='_blank'>static 必須在可見性后聲明。結構控制關鍵詞后必須有一個空格; 方法和函數必須沒有空格。結構控制的 { 必須在同一行,} 必須在body的下一行。 結構控制的 ( 后必須有空格, 結構控制的 ) 前必須沒有空格。

    例子

    下面是一個綜合的例子,可以幫助你對規則有一個概略的認識。

    <?phpnamespace Vendor/Package;use FooInterface;use BarClass as Bar;use OtherVendor/OtherPackage/BazClass;class Foo extends Bar implements FooInterface{    public function sampleFunction($a, $b = null)    {        if ($a === $b) {            bar();        } elseif ($a > $b) {            $foo->bar($arg1);        } else {            BazClass::bar($arg2, $arg3);        }    }    final public static function bar()    {        // 方法 body    }}

    基本規則

    基本編碼標準

    代碼必須遵循PSR-1的條款。

    文件所有文件必須使用 Unix LF (linefeed) 換行。所有PHP文件必須使用單個空行結束。只包含PHP的代碼必須忽略php結束標記 ? >。

    行行長度必須沒有硬性限制。長度的軟性限制必須為120字符;自動代碼風格檢查必須將120字設置為警告,必須不能設置為錯誤。行不應該超過80字符;超過這個長度的行應該切分為多個不超過80字符的行。非空行的結束必須沒有尾隨空格。為增強可讀性,可增加空行來標志代碼的關聯性。每行包含的語句必須不能超過1條。

    縮進代碼必須使用 4 空格的縮進, 必須不用tab作為縮進。

    注意:只使用空格,不用tab,有助于避免diffs,patches, history和annotations的問題。使用空格也有助于對齊。

    關鍵詞(保留字) 和 true/false/nullPHP保留字必須小寫.PHP常量 true, false和 null 必須小寫.

    Namespace 和 Use 聲明namespace 聲明之后必須有空行。所有use 聲明,必須在namespace聲明之后。每個聲明必須單獨使用一個use。use聲明區之后必須有一個空行。

    例如:

    <?phpnamespace Vendor/Package;use FooClass;use BarClass as Bar;use OtherVendor/OtherPackage/BazClass;// ... additional PHP code ...

    類, 屬性 和 方法

    這里的“類”包括 class、interface 和 trait。

    繼承 和 實現

    extends 和 implements 關鍵字必須和類名在同一行聲明。

    類的 { 必須獨占一行; } 必須在body的下一行。

    <?phpnamespace Vendor/Package;use FooClass;use BarClass as Bar;use OtherVendor/OtherPackage/BazClass;class ClassName extends ParentClass implements /ArrayAccess, /Countable{    // constants, properties, methods}

    implements 多個接口時,接口列表可以被分到多行,每個子行有一個縮進。如果這么做,第一個接口必須在implements 下一行,每行只能有一個接口。

    <?phpnamespace Vendor/Package;use FooClass;use BarClass as Bar;use OtherVendor/OtherPackage/BazClass;class ClassName extends ParentClass implements    /ArrayAccess,    /Countable,    /Serializable{    // constants, properties, methods}

    屬性

    所有屬性都必須聲明 visibility(可見性)。

    Var 關鍵字必須不能用于聲明屬性。

    每行必須只聲明一個屬性。

    不應該通過前綴下劃線來標示protected和private的屬性。

    例:

    <?phpnamespace Vendor/Package;class ClassName{    public $foo = null;}

    方法

    所有方法都必須聲明可見性。

    不應該通過前綴下劃線來標示protected和private的方法。

    聲明方法時,方法名的后必須沒有空格。 { 必須在同一行, } 必須在body的下一行。 (后必須沒有空格,) 前必須沒有空格。

    一個方法聲明的例子如下,注意 小括號,逗號,空格 和 花括號 的位置:

    <?phpnamespace Vendor/Package;class ClassName{    public function fooBarBaz($arg1, &$arg2, $arg3 = [])    {        // method body    }}

    方法參數

    方法的形參列表中, 每個逗號前必須沒有空格。有默認值的參數必須在參數列表的最后。

    <?phpnamespace Vendor/Package;class ClassName{    public function foo($arg1, &$arg2, $arg3 = [])    {        // method body    }}

    參數列表可以切分為多行,每個子行要有一個縮進。如果這么做,第一個參數必須獨占一行,每行只能有一個參數。

    參數列表切分為多行時,右括號)和左花括號{必須在同一行,之前必須有一個空格。

    <?phpnamespace Vendor/Package;class ClassName{    public function aVeryLongMethodName(        ClassTypeHint $arg1,        &$arg2,        array $arg3 = []    ) {        // method body    }}

    abstract, final 和 static

    abstract和final聲明必須在可見性之前聲明。
    static聲明必須在可見性之后。

    <?phpnamespace Vendor/Package;abstract class ClassName{    protected static $foo;    abstract protected function zim();    final public static function bar()    {        // method body    }}

    方法和函數調用

    寫方法或函數調用時,方法/函數名 和 左括號( 之間,必須沒有空格, 右括號 ) 之前必須沒有空格。在參數列表中,逗號間必須沒有逗號,每個逗號后必須有一個空格。

    <?php$foo->bar(    $longArgument,    $longerArgument,    $muchLongerArgument);

    控制結構

    控制結構通常遵循以下風格:

    控制結構關鍵詞后必須有一個空格。左括號后必須沒有空格。右括號前必須沒有空格。又括號和左花括號之間必須有一個空格。body必須有一層縮進。右花括號必須在body下一行。每個控制結構的body必須用花括號括起來。 即保證外觀統一,又減少了添加新行時引入的錯誤。

    if, elseif, else

    if 結構如下所示。注意括號、空格、花括號的位置;同時留意 else 和 elseif 與前一部分的 } 在同一行。

    <?phpif ($expr1) {    // if body} elseif ($expr2) {    // elseif body} else {    // else body;}

    elseif關鍵字不應該被 else if 代替。

    switch, case

    Switch結構如下所示。注意括號、空格和花括號的位置。 case 語句相對于switch必須有一個縮進, break關鍵字 或者其他終結性的關鍵字必須和case body在同一縮進層級。在非空的case body,如果沒有終結性語句,必須加上注釋 // no break。

    <?phpswitch ($expr) {    case 0:        echo 'First case, with a break';        break;    case 1:        echo 'Second case, which falls through';        // no break    case 2:    case 3:    case 4:        echo 'Third case, return instead of break';        return;    default:        echo 'Default case';        break;}

    while, do while

    while結構如下所示。 注意括號、空格和花括號的位置。

    <?phpwhile ($expr) {    // structure body}

    do-while接口如下所示。 注意括號、空格和花括號的位置。

    <?phpdo {    // structure body;} while ($expr);

    for

    for 結構如下所示。 注意括號、空格和花括號的位置。

    <?phpfor ($i = 0; $i < 10; $i++) {    // for body}

    foreach

    foreach 結構如下所示。 注意括號、空格和花括號的位置。

    <?phpforeach ($iterable as $key => $value) {    // foreach body}

    try, catch

    try-catch區塊如下所示。 注意括號、空格和花括號的位置。

    <?phptry {    // try body} catch (FirstExceptionType $e) {    // catch body} catch (OtherExceptionType $e) {    // catch body}

    Closure 閉包

    聲明閉包必須在function關鍵字后留一個空格,在use關鍵字前后各留一個空格。

    左花括號必須在同一行, 右花括號必須在body的下一行。

    參數或變量列表的左括號后 和 右括號前必須沒有空格。

    參數和變量列表的逗號前必須沒有空格,每個逗號后必須有一個空格。

    有默認值的參數必須排在最后。

    閉包的聲明如下所示。 注意括號,逗號,空格和花括號的位置:

    <?php$closureWithArgs = function ($arg1, $arg2) {    // body};$closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {    // body};

    參數列表和變量列表可以拆分到多行,每個子行有一層縮進。 這么做的時候,第一個列表成員必須獨占一行,每行只能有一個列表成員。

    參數或變量列表拆分為多行時,到了列表的末尾, 右括號 和 左花括號必須放在同一行,中間有一個空格。

    例子:

    <?php$longArgs_noVars = function (    $longArgument,    $longerArgument,    $muchLongerArgument) {   // body};$noArgs_longVars = function () use (    $longVar1,    $longerVar2,    $muchLongerVar3) {   // body};$longArgs_longVars = function (    $longArgument,    $longerArgument,    $muchLongerArgument) use (    $longVar1,    $longerVar2,    $muchLongerVar3) {   // body};$longArgs_shortVars = function (    $longArgument,    $longerArgument,    $muchLongerArgument) use ($var1) {   // body};$shortArgs_longVars = function ($arg) use (    $longVar1,    $longerVar2,    $muchLongerVar3) {   // body};

    注意:當閉包被直接作為函數或方法調用的參數時,以上規則同樣適用。

    <?php$foo->bar(    $arg1,    function ($arg2) use ($var1) {        // body    },    $arg3);

    結語

    本指南刻意忽略了許多風格和實踐。包括但不限于:

    聲明全局變量和全局常量。聲明函數。操作符和賦值。行間對齊。注釋和文檔區。類名前后綴。最佳實踐。

    Future recommendations MAY revise and extend this guide to address those or other elements of style and practice.

    附錄A 調查

    In writing this style guide, the group took a survey of member projects to determine common practices. The survey is retained herein for posterity.

    調查數據
    url,http://www.horde.org/apps/horde/docs/CODING_STANDARDS,http://pear.php.net/manual/en/standards.php,http://solarphp.com/manual/appendix-standards.style,http://framework.zend.com/manual/en/coding-standard.html,http://symfony.com/doc/2.0/contributing/code/standards.html,http://www.ppi.io/docs/coding-standards.html,https://github.com/ezsystems/ezp-next/wiki/codingstandards,http://book.cakephp.org/2.0/en/contributing/cakephp-coding-conventions.html,https://github.com/UnionOfRAD/lithium/wiki/Spec%3A-Coding,http://drupal.org/coding-standards,http://code.google.com/p/sabredav/,http://area51.phpbb.com/docs/31x/coding-guidelines.html,https://docs.google.com/a/zikula.org/document/edit?authkey=CPCU0Us&hgd=1&id=1fcqb93Sn-hR9c0mkN6m_tyWnmEvoswKBtSc0tKkZmJA,http://www.chisimba.com,n/a,https://github.com/Respect/project-info/blob/master/coding-standards-sample.php,n/a,Object Calisthenics for PHP,http://doc.nette.org/en/coding-standard,http://flow3.typo3.org,https://github.com/propelorm/Propel2/wiki/Coding-Standards,http://developer.joomla.org/coding-standards.htmlvoting,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,no,no,no,?,yes,no,yesindent_type,4,4,4,4,4,tab,4,tab,tab,2,4,tab,4,4,4,4,4,4,tab,tab,4,tabline_length_limit_soft,75,75,75,75,no,85,120,120,80,80,80,no,100,80,80,?,?,120,80,120,no,150line_length_limit_hard,85,85,85,85,no,no,no,no,100,?,no,no,no,100,100,?,120,120,no,no,no,noclass_names,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,studly,lower_under,studly,lower,studly,studly,studly,studly,?,studly,studly,studlyclass_brace_line,next,next,next,next,next,same,next,same,same,same,same,next,next,next,next,next,next,next,next,same,next,nextconstant_names,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,upper,uppertrue_false_null,lower,lower,lower,lower,lower,lower,lower,lower,lower,upper,lower,lower,lower,upper,lower,lower,lower,lower,lower,upper,lower,lowermethod_names,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel,camel,lower_under,camel,camel,camel,camel,camel,camel,camel,camel,camel,camelmethod_brace_line,next,next,next,next,next,same,next,same,same,same,same,next,next,same,next,next,next,next,next,same,next,nextcontrol_brace_line,same,same,same,same,same,same,next,same,same,same,same,next,same,same,next,same,same,same,same,same,same,nextcontrol_space_after,yes,yes,yes,yes,yes,no,yes,yes,yes,yes,no,yes,yes,yes,yes,yes,yes,yes,yes,yes,yes,yesalways_use_control_braces,yes,yes,yes,yes,yes,yes,no,yes,yes,yes,no,yes,yes,yes,yes,no,yes,yes,yes,yes,yes,yeselse_elseif_line,same,same,same,same,same,same,next,same,same,next,same,next,same,next,next,same,same,same,same,same,same,nextcase_break_indent_from_switch,0/1,0/1,0/1,1/2,1/2,1/2,1/2,1/1,1/1,1/2,1/2,1/1,1/2,1/2,1/2,1/2,1/2,1/2,0/1,1/1,1/2,1/2function_space_after,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,no,noclosing_php_tag_required,no,no,no,no,no,no,no,no,yes,no,no,no,no,yes,no,no,no,no,no,yes,no,noline_endings,LF,LF,LF,LF,LF,LF,LF,LF,?,LF,?,LF,LF,LF,LF,?,,LF,?,LF,LF,LFstatic_or_visibility_first,static,?,static,either,either,either,visibility,visibility,visibility,either,static,either,?,visibility,?,?,either,either,visibility,visibility,static,?control_space_parens,no,no,no,no,no,no,yes,no,no,no,no,no,no,yes,?,no,no,no,no,no,no,noblank_line_after_php,no,no,no,no,yes,no,no,no,no,yes,yes,no,no,yes,?,yes,yes,no,yes,no,yes,noclass_method_control_brace,next/next/same,next/next/same,next/next/same,next/next/same,next/next/same,same/same/same,next/next/next,same/same/same,same/same/same,same/same/same,same/same/same,next/next/next,next/next/same,next/same/same,next/next/next,next/next/same,next/next/same,next/next/same,next/next/same,same/same/same,next/next/same,next/next/next

    調查說明

    indent_type: The type of indenting. tab = “Use a tab”, 2 or 4 = “number of spaces”

    line_length_limit_soft: The “soft” line length limit, in characters. ? = not discernible or no response, no means no limit.

    line_length_limit_hard: The “hard” line length limit, in characters. ? = not discernible or no response, no means no limit.

    class_names: How classes are named. lower = lowercase only, lower_under = lowercase with underscore separators, studly = StudlyCase.

    class_brace_line: Does the opening brace for a class go on the same line as the class keyword, or on the next line after it?

    constant_names: How are class constants named? upper = Uppercase with underscore separators.

    true_false_null: Are the true, false, and null keywords spelled as all lower case, or all upper case?

    method_names: How are methods named? camel = camelCase, lower_under = lowercase with underscore separators.

    method_brace_line: Does the opening brace for a method go on the same line as the method name, or on the next line?

    control_brace_line: Does the opening brace for a control structure go on the same line, or on the next line?

    control_space_after: Is there a space after the control structure keyword?

    always_use_control_braces: Do control structures always use braces?

    else_elseif_line: When using else or elseif, does it go on the same line as the previous closing brace, or does it go on the next line?

    case_break_indent_from_switch: How many times are case and break indented from an opening switch statement?

    function_space_after: Do function calls have a space after the function name and before the opening parenthesis?

    closing_php_tag_required: In files containing only PHP, is the closing ?> tag required?

    line_endings: What type of line ending is used?

    static_or_visibility_first: When declaring a method, does static come first, or does the visibility come first?

    control_space_parens: In a control structure expression, is there a space after the opening parenthesis and a space before the closing parenthesis? yes = if ( expr),no=if(expr).

    blank_line_after_php: Is there a blank line after the opening PHP tag?

    class_method_control_brace: A summary of what line the opening braces go on for classes, methods, and control structures.

    調查結果
    indent_type:    tab: 7    2: 1    4: 14line_length_limit_soft:    ?: 2    no: 3    75: 4    80: 6    85: 1    100: 1    120: 4    150: 1line_length_limit_hard:    ?: 2    no: 11    85: 4    100: 3    120: 2class_names:    ?: 1    lower: 1    lower_under: 1    studly: 19class_brace_line:    next: 16    same: 6constant_names:    upper: 22true_false_null:    lower: 19    upper: 3method_names:    camel: 21    lower_under: 1method_brace_line:    next: 15    same: 7control_brace_line:    next: 4    same: 18control_space_after:    no: 2    yes: 20always_use_control_braces:    no: 3    yes: 19else_elseif_line:    next: 6    same: 16case_break_indent_from_switch:    0/1: 4    1/1: 4    1/2: 14function_space_after:    no: 22closing_php_tag_required:    no: 19    yes: 3line_endings:    ?: 5    LF: 17static_or_visibility_first:    ?: 5    either: 7    static: 4    visibility: 6control_space_parens:    ?: 1    no: 19    yes: 2blank_line_after_php:    ?: 1    no: 13    yes: 8class_method_control_brace:    next/next/next: 4    next/next/same: 11    next/same/same: 1    same/same/same: 6
    PHP編程

    鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

  • 發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 黄视频网站免费在线观看 | 羞羞视频入口 | 国产91精品久久久久久 | 毛片久久| 久久亚洲视频网 | 日韩黄色免费电影 | 国产欧美在线观看不卡一 | 久久99久久98精品免观看软件 | 韩毛片| 欧美成人久久 | 日韩视频中文 | 免费国产一级淫片 | 亚洲电影在线播放 | 黄色免费在线网站 | 伊人久操视频 | 国产成人精品免高潮在线观看 | 亚洲精品一区中文字幕 | 久久久99精品视频 | 久久99精品久久久久久236 | jizzyouxxxx| 免费啪视频在线观看 | www.射| 免费视频www在线观看 | 黄色毛片一级 | 欧洲精品视频在线观看 | 成人免费毛片在线观看 | 久草在线手机观看 | 免费看成人av | 涩涩屋av | 久久久久亚洲精品 | 成人福利视频在 | 男女羞羞视频在线免费观看 | 国产成人精品一区二区三区电影 | 国产一极毛片 | 怦然心动50免费完整版 | 成人综合一区二区 | 国产精品99久久久久久大便 | 国产在线91 | 538任你躁在线精品视频网站 | 91九色电影| 久久国产成人午夜av浪潮 |