很多時(shí)候,我們可以看到國(guó)外的框架有有很多特別的寫法,但是我們卻不明其為什么要這么寫,其實(shí)他們都是規(guī)范于PSR標(biāo)準(zhǔn)。
PSR標(biāo)準(zhǔn),是又PHP-FIG制定的PHP編碼規(guī)范
概述
從官方我們可以看到,目前審核通過(guò)的規(guī)范一共有8套。規(guī)范1-4常常被我們?cè)诟鞣N文檔中看到,但是這些規(guī)范的包含的什么,我們卻很少知道,這里我就簡(jiǎn)單的整理一下。

PSR-0 Basic Coding Standard 基本編碼標(biāo)準(zhǔn)
標(biāo)準(zhǔn)內(nèi)容
1.概述
- 文件 必須 用
<?php和<?=作為開始或者結(jié)束標(biāo)簽。 - PHP代碼文件 必須 使用UTF-8不帶BOM頭的編碼格式。
- 文件 應(yīng)該 聲明出代碼的類型(比如 function,class,constants,其他...) 或者 聲明出會(huì)產(chǎn)生的副作用(輸出內(nèi)容,更改配置)之一。而且不能兩個(gè)聲明一起出現(xiàn)。
- 命名空間和類 必須 符合一個(gè)自動(dòng)加載規(guī)范 PSR-0或者PSR-4。注: PIG官方已經(jīng)標(biāo)注PSR-0為過(guò)時(shí)的規(guī)范。
- class名 必須 聲明為
StudlyCaps。 大駝峰(首字母大寫的駝峰) - method名 必須 聲明為
camelCase。小駝峰(首字母小寫的駝峰)
2.1 PHP Tags
PHP代碼 必須 使用長(zhǎng)標(biāo)簽 <php ?> 或者短標(biāo)簽<?= ?>
2.2 Character Encoding
PHP代碼 必須 使用 不帶BOM頭的UTF-8編碼
2.3 Side Effects
聲明文件 應(yīng)該 不包含會(huì)有產(chǎn)生副作用的代碼。有產(chǎn)生副作用的代碼也不應(yīng)該包含有聲明的情況。
“Side effects” include but are not limited to: generating output, explicit use of require
or include
, connecting to external services, modifying ini settings, emitting errors or exceptions, modifying global or static variables, reading from or writing to a file, and so on.
副作用:包含但不僅限于生成輸出,包含require或者include,連接外部服務(wù)器,改變php.ini設(shè)置,限制錯(cuò)誤信息輸出,改變?nèi)只蛘哽o態(tài)變量,讀取或者寫入文件。
下面是一個(gè)不遵守這規(guī)范的例子
<?php
// side effect: change ini settings 副作用:改變ini設(shè)置
ini_set('error_reporting', E_ALL);
// side effect: loads a fileinclude "file.php"; 副作用:包含文件
// side effect: generates output 副作用:生成輸出
echo "<html>\n";
// declaration 定義:函數(shù)
function foo(){
// function body
}
下面是一個(gè)符合標(biāo)準(zhǔn)的例子,定義一個(gè)函數(shù),沒有產(chǎn)生副作用的代碼
<?php
// declaration 聲明foo函數(shù)
function foo(){
// function body
}
// conditional declaration is *not* a side effect 條件判斷不會(huì)產(chǎn)生副作用
if (! function_exists('bar')) {
function bar() {
// function body
}
}
2.4 Namespace and Class Names
Namespaces和classes名 必須 符合一種自動(dòng)加載機(jī)制。PSR-0或者PSR-4
這其中包含了: 每一個(gè)class必須獨(dú)占一個(gè)文件
class names 必須 被命名為 ```StudlyCaps````.
如果代碼運(yùn)行環(huán)境是PHP5.3或者更新的,必須使用格式化的namespaces
舉個(gè)栗子
<?php
// PHP 5.3 and later:
namespace Vendor\Model;
class Foo
{
}
運(yùn)行環(huán)境是5.2或者更古老的,應(yīng)該 使用偽命名空間
<?php
// PHP 5.2.x and earlier:
//在PSR-0中 下劃線"_"會(huì)自動(dòng)被轉(zhuǎn)換成DS "/".用這個(gè)原理來(lái)自動(dòng)加載Vendor/Model/Foo文件
class Vendor_Model_Foo
{
}
2.5 Class Constants, Properties, and Methods
這里的"class"指所有的 classer, interfaces, traits
-
Constants 常量
class常亮 必須被定義為全大寫
<?php
namespace Vendor\Model;
class Foo
{
const VERSION = '1.0';
const DATE_APPROVED = '2012-06-01';
}
- Properties 特性
This guide intentionally avoids any recommendation regarding the use of$StudlyCaps
, $camelCase
, or $under_score
property names.
Whatever naming convention is used SHOULD be applied consistently within a reasonable scope. That scope may be vendor-level, package-level, class-level, or method-level.
我大概把他理解為 每一種命名規(guī)范至少在某一組織下保持一致
- Methods 方法
方法名 必須 聲明為camelCase
這里就是PSR-1規(guī)范的全部?jī)?nèi)容了,其中classes需要聯(lián)系著PSR-0或者PSR-4來(lái)看。