第13篇 PSR-2代碼樣式

這個規範原文以下:php

1. Overview

  • Code MUST follow a "coding style guide" PSR [PSR-1].git

  • Code MUST use 4 spaces for indenting, not tabs.github

  • There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.sql

  • There MUST be one blank line after the namespace declaration, and there MUST be one blank line after the block of use declarations.閉包

  • Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.less

  • Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body.編輯器

  • Visibility MUST be declared on all properties and methods; abstract and finalMUST be declared before the visibility; static MUST be declared after the visibility.ide

  • Control structure keywords MUST have one space after them; method and function calls MUST NOT.函數

  • Opening braces for control structures MUST go on the same line, and closing braces MUST go on the next line after the body.ui

  • Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before.

 

這一規範主要是約束代碼風格的,但是說是全部裏面最關鍵最重要的,也是須要好好規範和共同遵照的。

 

咱們一個個來看下,只能我大略的寫一些比較重要的,或者說平時用的最多的。

 

1. 源文件

  1. 文件末尾必須空一行。
  2. 必須使用Unix LF(換行)做爲行結束符。
  3. 純PHP代碼源文件的關閉標籤?>必須省略。

第3點實際上是蠻重要的,我以前還老寫閉合,如今不寫了。這能夠避免在 PHP 結束標記以後萬一意外加入了空格或者換行符,會致使 PHP 開始輸出這些空白,而腳本中此時並沒有輸出的意圖。

 

2. 縮進

必須使用4個空格來縮進,不能使用Tab鍵。固然你若是把Tab在編輯器裏手動設置爲4個空格也能夠。這樣的目的是由於:每一個人的機器上的Tab鍵都不同,有些是4個空格,有些是8個空格,在你的機器上看着很爽的代碼,在別人機器上了就各類噁心了。因此,統一搞成4個空格,無論在哪裏打開都是美觀的。

 

3. 行

一行推薦的是最多寫80個字符,多於這個字符就應該換行了,通常的編輯器是能夠設置的。

 

4. 關鍵字和 True/False/Null

php的關鍵字,必須小寫,boolean值:true,false,null 也必須小寫

下面是php的keyword,必須小寫。

'__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor'

 

5. 命名空間(Namespace)和導入(Use)聲明

先簡單文字描述下:

命名空間(namespace)的聲明後面必須有一行空行。

全部的導入(use)聲明必須放在命名空間(namespace)聲明的下面。

一句聲明中,必須只有一個導入(use)關鍵字。

在導入(use)聲明代碼塊後面必須有一行空行。

用代碼來講明下:

 

<?php

namespace Lib\Databases; //下面必須空格一行

 

class Mysql {

}

namespace下空一行,才能使用use,再空一行,才能聲明class

 

<?php

namespace Lib\Databases; // 下面必須空格一行

 

use FooInterface; //use 必須在namespace 後面聲明

use BarClass as Bar;

use OtherVendor\OtherPackage\BazClass; // 下面必須空格一行

 

class Mysql

{

}

 

6. 類(class),屬性(property)和方法(method)

1 . 繼承(extends) 和實現(implement) 必須和 class name 寫在一行,切花括號要換行寫。

 

<?php

namespace Lib\Databaes;

 

class Mysql extends ParentClass implements \PDO, \DB // 寫一行

{ //換行寫{

}

2 . 屬性(property)必須聲明其可見性,究竟是 public 仍是protected仍是 private,不能省略,也不能使用var, var是php老版本中的什麼方式,等用於public.

 

<?php

namespace Lib\Databaes;

 

class Mysql extends ParentClass implements \PDO, \DB // 寫一行

{

public $foo = null;

private $name = 'yangyi';

protected $age = '17';

}

3 . 方法(method) ,必須 聲明其可見性,究竟是 public 仍是protected仍是 private,不能省略。而且,花括號{必須換行寫。若是有多個參數,第一個參數後緊接, ,再加個空格,且函數name和( 之間必需要有個空格:function_name ($par, $par2, $pa3), 若是參數有默認值,也要用左右空格分開。

 

<?php

namespace Lib\Databaes;

 

class Mysql extends ParentClass implements \PDO, \DB // 寫一行

{

public getInfo ($name, $age, $gender = 1) //函數名getInfo和(之間有個空格,參數之間也要有空格。默認參數也要左右都有空格

{ //必須換行寫{

}

}

4 . 當用到抽象(abstract)和終結(final)來作類聲明時,它們必須放在可見性聲明 (public 仍是protected仍是private)的前面。而當用到靜態(static)來作類聲明時,則必須放在可見性聲明的後面。

直接上代碼:

 

<?php

namespace Vendor\Package;

abstract class ClassName

{

protected static $foo; //static放後面

abstract protected function zim(); //abstract放前面

final public static function bar() //final放前面,static放最後。

{

// 方法主體部分

}

}

 

7.控制結構

控制接口,就是if else while switch等。這一類的寫法規範也是常常容易出現問題的,也要規範一下。

1 . if,elseif,else寫法,直接上規範代碼吧:

 

<?php

if ($expr1) { //左右空格

// if body

} elseif ($expr2) { //elesif 連着寫

// elseif body

} else {

// else body;

}

2 . switchcase 注意左右空格和換行,仍是直接上規範代碼:

 

<?php

switch ($expr) { //左右空格

case 0:

echo 'First case, with a break'; //對其

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;

}

3 . whiledo while 的寫法也是相似,要左右空格,上代碼:

 

<?php

while ($expr) { //左右空格

// structure body

}

do {

// structure body; //左右空格

} while ($expr);

4 . for的寫法

 

<?php

for ($i = 0; $i < 10; $i++) { //注意幾個參數之間的空格

// for body

}

5 . foreach的寫法

 

<?php

foreach ($iterable as $key => $value) { //仍是空格問題

// foreach body

}

6 . try catch的寫法

 

<?php

try {

    // try body

} catch (FirstExceptionType $e) { //一樣也是注意空格。

    // catch body

} catch (OtherExceptionType $e) {

    // catch body

}

基本用到的就是這些了,其餘什麼閉包啥的用的很少就不過多的累述了。

相關文章
相關標籤/搜索