> For the complete documentation index, see [llms.txt](https://doc.shoumaitec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.shoumaitec.com/shang-cheng-gui-fan/chang-yong-dai-ma-gui-fan.md).

# 常用代码规范

&#x20;所有 PSR 规范请见：<https://learnku.com/docs/psr>&#x20;

## 想细看请点击下面链接， 想偷懒请看下面demo

## [「PSR 规范」PSR-1 基础编码规范](https://learnku.com/laravel/t/2078/psr-specification-psr-1-basic-coding-specification)

## [「PSR 规范」PSR-2 编码风格规范](https://learnku.com/laravel/t/2079/psr-specification-psr-2-coding-style-specification)

## [「PSR 规范」PSR-3 日志接口规范](https://learnku.com/laravel/t/2080/psr-specification-psr-3-log-interface-specification)

## [「PSR 规范」PSR-4 自动加载规范](https://learnku.com/laravel/t/2081/psr-specification-psr-4-automatic-loading-specification)

## 来自[PSR-1](https://learnku.com/laravel/t/2078/psr-specification-psr-1-basic-coding-specification)

命名空间以及类的命名**必须**遵循[PSR-4](https://learnku.com/docs/psr/psr-4-autoloader/1608)

### &#x20;类的命名 **必须** 遵循 `StudlyCaps` 大写开头的驼峰命名规范。

### &#x20;类的常量中所有字母都 **必须** 大写，词间以下划线分隔。

### &#x20;类的属性命名建议 遵循：小写开头的驼峰式（$camelCase）

### &#x20;方法名称 **必须** 符合 `camelCase()` 式的小写开头驼峰命名规范。

```
<?php
// PHP 5.3及以后版本的写法
namespace Vendor\Model;

class Foo
{
    const VERSION = '1.0';
    const DATE_APPROVED = '2012-06-01';
}
```

## 来自[PSR-2](https://learnku.com/laravel/t/2079/psr-specification-psr-2-coding-style-specification)

### PHP 所有 [关键字](http://php.net/manual/en/reserved.keywords.php) **必须** 全部小写。常量 `true` 、`false` 和 `null` 也 **必须** 全部小写。

### &#x20;`namespace` 声明后 **必须** 插入一个空白行。

### &#x20;所有 `use` **必须** 在 `namespace` 后声明。

### &#x20;每条 `use` 声明语句 **必须** 只有一个 `use` 关键词。

### &#x20;`use` 声明语句块后 **必须** 要有一个空白行

### &#x20;关键词 `extends` 和 `implements` **必须** 写在类名称的同一行。

### &#x20;类的开始花括号 **必须** 独占一行，结束花括号也 **必须** 在类主体后独占一行。

### &#x20;`implements` 的继承列表也 **可以** 分成多行，这样的话，每个继承接口名称都 **必须** 分开独立成行，包括第一个。

### &#x20;每个属性都 **必须** 添加访问修饰符。

### &#x20;所有方法都 **必须** 添加访问修饰符。

### &#x20;参数列表中，每个逗号后面 **必须** 要有一个空格，而逗号前面 **一定不可** 有空格。

### &#x20;需要添加 `abstract` 或 `final` 声明时，**必须** 写在访问修饰符前，而 `static` 则 **必须** 写在其后。

### &#x20;方法及函数调用时，方法名或函数名与参数左括号之间 **一定不可** 有空格，参数右括号前也 **一定不可** 有空格。每个参数前 **一定不可** 有空格，但其后 **必须** 有一个空格

### **后面不写了 看下面例子吧。！！！！！**

```
<?php
namespace Vendor\Package;

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

class Foo extends Bar implements 
    FooInterface
    \ArrayAccess,
    \Countable,
    \Serializable
{
    public $foo = null;
    protected static $foo;
    
    public function sampleFunction($a, $b = null)
    {
        if ($a === $b) {
            bar();
        } elseif ($a > $b) {
            $foo->bar($arg1);
        } else {
            BazClass::bar($arg2, $arg3);
        }
   
        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
        }
        
        $closureWithArgs = function ($arg1, $arg2) {
            // body
        };

        $closureWithArgsAndVars = function ($arg1, $arg2) use ($var1, $var2) {
            // body
        };
        
        $longArgs_noVars = function (
            $longArgument,
            $longerArgument,
            $muchLongerArgument
        ) {
           // body
        };

        $noArgs_longVars = function () use (
            $longVar1,
            $longerVar2,
            $muchLongerVar3
        ) {
           // body
        };

        $longArgs_longVars = function (
            $longArgument,
            $longerArgument,
            $muchLongerArgument
        ) use (
            $longVar1,
            $longerVar2,
            $muchLongerVar3
        ) {
           // body
        };

        $longArgs_shortVars = function (
            $longArgument,
            $longerArgument,
            $muchLongerArgument
        ) use ($var1) {
           // body
        };

        $shortArgs_longVars = function ($arg) use (
            $longVar1,
            $longerVar2,
            $muchLongerVar3
        ) {
           // body
        };
        
        $foo->bar(
            $arg1,
            function ($arg2) use ($var1) {
                // body
            },
            $arg3
        );
    }

    final public static function bar()
    {
        // 方法的内容
    }
    
    public function aVeryLongMethodName(
        ClassTypeHint $arg1,
        &$arg2,
        array $arg3 = []
    ) {
        // 方法的内容
    }
    
    abstract protected function zim();

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

bar();  //方法调用
$foo->bar($arg1);
Foo::bar($arg2, $arg3);
```
