PHP代码风格指南

作者: Kaiyulee | 来源:发表于2015-03-11 00:17 被阅读698次

PHP编码规范

PSR-2

概要

  • 务必要遵循 PSR-1 规范;
  • 4个空格缩进(编辑器设置下Tab键占4个空格即可);
  • 每行字符不得超过120个,最好控制在80内;
  • 声明namespace后空一行;
  • 声明use后空一行,多个use之间不空行;
  • classmethod的开始{与结束}括号必须独自占一行;
  • class的属性与方法可见性必须注明,且abstractfinal必须在public等一般可见性之前,static排最后。
  • 控制结构中关键词如if后面必须有一个空格,而函数或者方法后面不能跟空格,如funName()
  • 控制结构中的开始{与语句在同一行,而结束}须另起一行;
  • 控制结构中的开始圆括号后不能有空格,结束圆括号之前也不能有空格。

Examples

<?php
namespace 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()
    {
        // method body
    }
}

<?php
namespace Vendor\Package;

use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class ClassName extends ParentClass implements \ArrayAccess, \Countable
{
    // constants, properties, methods
}

<?php
namespace Vendor\Package;

use FooClass;
use BarClass as Bar;
use OtherVendor\OtherPackage\BazClass;

class ClassName extends ParentClass implements
    \ArrayAccess,
    \Countable,
    \Serializable
{
    // constants, properties, methods
}

<?php
namespace Vendor\Package;

class ClassName
{
    public $foo = null;
}

<?php
namespace Vendor\Package;

class ClassName
{
    public function fooBarBaz($arg1, &$arg2, $arg3 = [])
    {
        // method body
    }
    // 参数较多的话可以这样:
    public function aVeryLongMethodName(
        ClassTypeHint $arg1,
        &$arg2,
        array $arg3 = []
    ) {
        // method body
    }
}

<?php
namespace Vendor\Package;

abstract class ClassName
{
    protected static $foo;

    abstract protected function zim();

    final public static function bar()
    {
        // method body
    }
}

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

switch ($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 ($expr) {
    // structure body
}

do {
    // structure body;
} while ($expr);

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

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

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

有省略。详细参考 PSR-2

相关文章

网友评论

    本文标题:PHP代码风格指南

    本文链接:https://www.haomeiwen.com/subject/rstbxttx.html