这个规范原文以下:php
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 final
MUST 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.
这一规范主要是约束代码风格的,但是说是全部里面最关键最重要的,也是须要好好规范和共同遵照的。
咱们一个个来看下,只能我大略的写一些比较重要的,或者说平时用的最多的。
?>
必须省略。第3点实际上是蛮重要的,我以前还老写闭合,如今不写了。这能够避免在 PHP 结束标记以后万一意外加入了空格或者换行符,会致使 PHP 开始输出这些空白,而脚本中此时并没有输出的意图。
必须使用4个空格来缩进,不能使用Tab键。固然你若是把Tab在编辑器里手动设置为4个空格也能够。这样的目的是由于:每一个人的机器上的Tab键都不同,有些是4个空格,有些是8个空格,在你的机器上看着很爽的代码,在别人机器上了就各类恶心了。因此,统一搞成4个空格,无论在哪里打开都是美观的。
一行推荐的是最多写80个字符,多于这个字符就应该换行了,通常的编辑器是能够设置的。
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'
先简单文字描述下:
命名空间(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
{
}
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放最后。
{
// 方法主体部分
}
}
控制接口,就是if
else
while
switch
等。这一类的写法规范也是常常容易出现问题的,也要规范一下。
1 . if,elseif,else写法,直接上规范代码吧:
<?php
if ($expr1) { //左右空格
// if body
} elseif ($expr2) { //elesif 连着写
// elseif body
} else {
// else body;
}
2 . switch
,case
注意左右空格和换行,仍是直接上规范代码:
<?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 . while
,do 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
}
基本用到的就是这些了,其余什么闭包啥的用的很少就不过多的累述了。