PHP编码风格规范

  • 2019 年 10 月 3 日
  • 笔记

??PHP?????????????????????????????????PHP??????????PSR????PSR-1?PSR-2??????PHP??????????????????????????????????????????????????????????????PSR???????????PSR-1?PSR-2??????????

????PSR

PSR?? ?? ??
1 ?????? ??PHP???????????????
2 ?????? ?????????????????????
3 ?????? ??????????????????
4 ?????? ?????????????????????????????
6 ?????? ?????????????????????????????????
7 HTTP?????? ??HTTP????????

PSR-1??????

  1. ????
    ???PHP?????<?php?<?=????????????????????<?????????
  2. ???
    PHP???????????????????????(??????????????)????????????
  3. ??
    ?????????????????????????????????????????????????????????????????????????????????????????????????????????

PSR-2??????

  1. PSR-1??PHP???<?php???PSR-2????PHP?????????>?????????????????

  2. namespace?????????????use??????????????????????????????use???

  3. ???????
    class?????????extends?implements???????????????????????????????????????????????????????????????????????????????????????implements?????????????({)???????????????????(})????????????????????????????

    class EarthGame extends Game implements      Playable,      Savable  {      //??  }

    ?????????????????

    class EarthGame extends Game implements Playble, Savable  {       //??  }
  4. ????
    ?????????????(public?private?protected)?????????var?????PSR-1????????????:??????????????????????????????????(?????????????)

  5. ????????
    ?????????????(public?private?protected)?????????abstract?final???static?????????????????????????

    • ????
      ????????({)??????????????????(})?????????????(??????????)??????????????????(??????????????)???????????(????)????????????????????????????????????:

      final public static function generateTile(int $diamondCount, bool $polluted = false)  {     //???  }
    • ????
      ????????????????????????????????????????????(????????????????)???????????????????????????????????????????????????????({)????????????????????????????????????????????????????????????????

      public function __construct(      int $size,      string $name,      bool $warparound = false,      bool $aliens = false  ) {    //???  }
  6. ????
    ??????4????????????????????????????????????Tab????4??????????????????????120????

  7. ???????
    ??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

    $earthGanme = new EarthGame(        5,        'earth',        true,        true  );  $earthGame::generateTile(5, true);
  8. ????
    ???????(if?for?while?)??????????????????????????????????????????????????????????(??)???????????????????????????????????????????????????????

    $title = [];  for ($x = 0; $x < $diamondCount; $x++) {      if ($polluted) {          $title[] = new PollutionDecorator(new DiamondDecorator(new Plains()));      } else {          $title[] = new DiamondDecorator(new Plains());      }  }

    ??if?for??????for?if????????????????????????????????????????????