第13篇 PSR-2代码样式
2016-02-01 13:07
696 查看
这个规范原文如下:
Code MUST use 4 spaces for indenting, not tabs.
There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.
There MUST be one blank line after the
Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.
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;
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.
Opening parentheses for control structures MUST NOT have a space after them, and closing parentheses for control structures MUST NOT have a space before.
这一规范主要是约束代码风格的,可是说是所有里面最关键最重要的,也是需要好好规范和共同遵守的。
我们一个个来看下,只能我大略的写一些比较重要的,或者说平时用的最多的。
必须使用Unix LF(换行)作为行结束符。
纯PHP代码源文件的关闭标签
第3点其实是蛮重要的,我之前还老写闭合,现在不写了。这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图。
下面是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)声明代码块后面必须有一行空行。
用代码来说明下:
namespace下空一行,才能使用use,再空一行,才能声明class
2 . 属性(property)必须声明其可见性,到底是
3 . 方法(method) ,必须 声明其可见性,到底是
4 . 当用到抽象(abstract)和终结(final)来做类声明时,它们必须放在可见性声明 (
直接上代码:
1 . if,elseif,else写法,直接上规范代码吧:
2 .
3 .
4 .
5 .
6 .
基本用到的就是这些了,其他什么闭包啥的用的不多就不过多的累述了。
1. Overview
Code MUST follow a "coding style guide" PSR [PSR-1].Code MUST use 4 spaces for indenting, not tabs.
There MUST NOT be a hard limit on line length; the soft limit MUST be 120 characters; lines SHOULD be 80 characters or less.
There MUST be one blank line after the
namespacedeclaration, and there MUST be one blank line after the block of
usedeclarations.
Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body.
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;
abstractand
finalMUST be declared before the visibility;
staticMUST be declared after the visibility.
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.
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. 源文件
文件末尾必须空一行。必须使用Unix LF(换行)作为行结束符。
纯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 .
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
}
基本用到的就是这些了,其他什么闭包啥的用的不多就不过多的累述了。
相关文章推荐
- Qt窗口主题样式切换功能
- css实现iframe的高度自适应
- 详解CSS的结构与层叠以及格式化
- 网页制作案例
- CSS制作水平垂直居中对齐
- css 居中
- HTML(5)/CSS(3)回顾(一)之CSS基础(一)
- CSS3 新功能
- CSS 参考文档
- Web之旅第四站——CSS(Cascading style Sheet) 层叠样式表
- CSS 样式注意、常识
- 初识CSS3 3D效果,浅谈理解
- CSS
- HTML/CSS
- 关于CSS中的元素定位
- css3 3d 学习文档
- web前端基础——初识CSS
- 【CSS】盒子模型 之 IE 与W3C的盒子模型对比
- 【css】盒子模型 之 概述
- 黑马12期day01之html&css